dotnetic Posted June 27, 2018 Share Posted June 27, 2018 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 More sharing options...
adrian Posted June 27, 2018 Share Posted June 27, 2018 Take a read through this thread: https://processwire.com/talk/topic/5397-assigning-fields-to-repeaters-with-the-api/ I think the function by @thetuningspoon does what you need, but please read my comment afterwards - not sure if that change was incorporated or not. 3 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now