Carbonfields, undefined number of elements

159 Views Asked by At

I am using carbon fields for a wordpress plugin. The problem is that in the options there should be an undefined number of inputs.

For instance (this is a simplification), in the settings there is "people", where the developer can specify more than one people.

For instance, the fields should be:

- Name
  Surname
  x (delete)
- Name
  Surname
  x (delete)
+ (add new person)

When + is clicked, new fields Name, Surname, x should appear.

My code is now something like:

 Container::make('theme_options', 'XXX')
            ->set_page_parent('options-general.php')
            ->add_tab( 'People', array(
                Field::make('text', 'xxx_name', 'Name'),
                Field::make('text', 'xxx_surname', 'Surname')
            ))

I want that the configuration is stored in an array

1

There are 1 best solutions below

0
On BEST ANSWER

There is already the complex field in carbon fields.

Complex fields act as containers to which you can add multiple groups of fields. It is represented as a table, where each row is a field group. The user is able to add infinite rows of each group. This allows to repeat a set of fields multiple times creating customizable and sortable lists. This is useful when creating image galleries, lists of data or advanced content and layout elements.

This is the link to the documentation page

In my case it would be something like:

Container::make('theme_options', 'XXX')
    ->set_page_parent('options-general.php')
    ->add_tab('People', array(
        Field::make('complex', 'xxx-person', 'Person')
            ->add_fields(array(
                Field::make('text', 'xxx_name', 'Name'),
                Field::make('text', 'xxx_surname', 'Surname')
            ))
    ));