arjen Posted July 8, 2013 Share Posted July 8, 2013 Hi, I'm working on some automation of repetitive tasks. I'm creating a module which creates a couple of fields and a repeater to hold those fields. I'm running into something I don't understand. This is my code: <?php // Create an array to store the repeaterField data $fields = array(); // Create the body field $f = new Field(); $f->type = wire()->modules->get("FieldtypeText"); $f->name = "subject"; $f->save(); // Add the field to the array array_push($fields, $f->id); // Create the body field $f = new Field(); $f->type = wire()->modules->get("FieldtypeText"); $f->name = "author"; $f->save(); // Add the field to the array array_push($fields, $f->id); // Create a repeater field $f = new Field(); $f->type = wire()->modules->get("FieldtypeRepeater"); $f->name = "quotes"; $f->repeaterFields = $fields; // Save the array as repeaterFields $f->save(); So far, so good. All fields get made and are available in the CMS. However, when looking in the CMS the $fields array seems empty, because there seems to be no fields assigned (see screenshot below). However when checking directly in the API using: print_r(wire()->fields->get('repeater')->repeaterFields); It does display the original values (the id of the subject and the id of the author). When editing the repeater field through the CMS and assigning a field it overwrites the repeaterFields value. From that point on everything works as expected. There has to be a step I'm missing. Can anyone help me out? Many thanks in advance. Link to comment Share on other sites More sharing options...
Soma Posted July 8, 2013 Share Posted July 8, 2013 What is this? $f->repeaterFields = $repeaterFields; Where does $repeaterFields come from? Also shouldn't it be something like: $f->repeaterFields->add($field); Link to comment Share on other sites More sharing options...
arjen Posted July 8, 2013 Author Share Posted July 8, 2013 (edited) Thanks soma! That was a typo. It should be $fields. I updated the code. Regarding the: $f->repeaterFields->add($field); I'm not sure I'm following you. How can this be converted to hold multiple fields? When trying to add one $f (field) I'm getting an error: Fatal error: Call to a member function add() on a non-object. Edited July 8, 2013 by arjen Link to comment Share on other sites More sharing options...
Soma Posted July 8, 2013 Share Posted July 8, 2013 I'm not sure about repeaters API to create a new fields and repeater. repeaterFields doesn't seem really the way to add fields. This is only used on the field config UI to add and remove fields. So it exists but it's processed through a POST. Repeaters use a template/fieldgroup where the fields are added. So a way to add fields I found by trying is after saving the repeater you get the template used by this repeater with the prefix "repeater" and the fieldname $t = $fieldgroups->get("repeater_quotes"); $t->add("title"); $t->save(); 2 Link to comment Share on other sites More sharing options...
Soma Posted July 8, 2013 Share Posted July 8, 2013 Possible that you need to set both, also the repeaterFields. But have no idea. 1 Link to comment Share on other sites More sharing options...
arjen Posted July 8, 2013 Author Share Posted July 8, 2013 Thanks for your time soma! I will look into this tomorrow. Perhaps the $f->repeaterFields route isn't the way to go. Link to comment Share on other sites More sharing options...
adrian Posted October 10, 2013 Share Posted October 10, 2013 Hey arjen, I don't suppose you solved this did you - I have having the same issue - looking through the DB tables I am yet to find anything obviously different from a repeater created via the admin vs one created via the API - will keep looking, but am hoping you might already have the answer EDIT: always the way - as soon as you post, you figure it out - the entries are missing from the fieldgroups_fields table. Hopefully should be easy to sort out! Link to comment Share on other sites More sharing options...
arjen Posted October 10, 2013 Author Share Posted October 10, 2013 I spend another few hours, but didn't really hit the nail. Coincidentally I was looking into this today again since it is still on my todo list. Will post an update in the next few days. Link to comment Share on other sites More sharing options...
adrian Posted February 25, 2014 Share Posted February 25, 2014 Just to wrap this up, here is a function that should do everything needed to set up a repeater and assign fields to it: http://processwire.com/talk/topic/5397-assigning-fields-to-repeaters-with-the-api/?p=52123 1 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