Jump to content

Add field/s to repeater via API


dotnetic
 Share

Recommended Posts

I want to create a repeater field via API and add fields to it.

I read that there is a property repeaterFields, but when I try to add a field it does not appear in the ProcessWire backend.

I put this code into my tracy console:

$fields->delete($fields->get('quiz_answers'));
$fields->delete($fields->get('quiz_correct_answer'));

$repeater = new Field;
$repeater->type = wire('modules')->get('FieldtypeRepeater');
$repeater->name = 'quiz_answers';
$repeater->label = 'Answer';
$repeater->repeaterCollapse = 3;
$repeater->required = true;
$repeater->repeaterAddLabel = 'Neue Antwort hinzufügen';
// $field->parent_id = wire('pages')->get("name=for-field-{$field->id}")->id;
// $field->template_id = $template->id;
$repeater->save();

$checkbox = new Field;
$checkbox->type = $this->modules->get("FieldtypeCheckbox");
$checkbox->name = "quiz_correct_answer";
$checkbox->label ="This is the correct answer";
$checkbox->tags = 'quiz';
$checkbox->save();

//add fields to repeaterFields
$repeaterFieldsArray = array();
$repeaterFieldsArray[] = $checkbox->id;
$repeater->repeaterFields = $repeaterFieldsArray;
$repeater->save();

But when I go and edit the repeater field 'quiz_answers', no field appears at details -> repeater fields.

What is the correct way to add one ore more fields to the repeater via API?

Maybe @teppo or @adrian could help me out with this, as you both seem to have some experience with repeater fields?

Parts of my code are stolen, copied inspired by https://gist.github.com/teppokoivula/8889040 and https://github.com/adrianbj/ProcessAdminActions/blob/master/actions/PageTableToRepeaterMatrix.action.php#L227

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...