Raymond Geerts Posted February 2, 2017 Share Posted February 2, 2017 Can any one point me in the right direction / or give a working example how to add repeater items to a repeater field inside a repeater matrix field. I tried many different approaches, reading many topics on the forum, but can not get it working. The following example gives an error about not being able to add page with ID 0 $rpm = $page->repeater_matrix_content->getNew(); $prm->repeater_matrix_type = 1; $prm->save(); $prm->of(false); foreach($videos as $video) { if (empty($video['url'])) continue; $rp = $prm->repeater_videos->getNew(); $rp->save(); $rp->link_url = $video['url']; $rp->save(); $prm->repeater_videos->add($rp); } $prm->save(); $page->repeater_matrix_content->add($prm); Link to comment Share on other sites More sharing options...
BitPoet Posted February 2, 2017 Share Posted February 2, 2017 (edited) Remove the line where you call $prm->repeater_videos->add. getNew() already adds the item, you just need to fill and save it. Edit: the same should be true for $page->repeater_matrix_content->add Edited February 2, 2017 by BitPoet 3 Link to comment Share on other sites More sharing options...
Raymond Geerts Posted February 6, 2017 Author Share Posted February 6, 2017 Thanks @BitPoet it works very well. I removed the ->add from all my code and all other repeater matrix items are still 'added' just fine by getNew(). Link to comment Share on other sites More sharing options...
Raymond Geerts Posted February 13, 2017 Author Share Posted February 13, 2017 Still having some difficulties here when updating a repeater inside a repeater matrix. Somehow it is not possible to use the same code for adding a new page, and updating the same page with a repeater inside a repeater matrix, as shown in the below example. I do not seem to be able to update the page, and keep getting the following error message: Error: Exception: Can't save page 0: /1486742936-8959-4/: It has no parent assigned (in /Users/raymond/Dropbox/www_domains/testing123/htdocs/wire/core/PagesEditor.php line 464) The code to add a new page, and data to a repeater inside a repeater matrix. $urls = array( 'https://www.youtube.com/watch?v=e5gvzCOhBLQ', 'https://www.youtube.com/watch?v=rbhnkeh9liQ', 'https://www.youtube.com/watch?v=poRFOvxyOWI' ); /** * First run, adds new article page * */ $options = array( 'template' => 'article', 'parent' => $parent = $pages->get('template=articles'), 'title' => 'This is a test', 'date_pub_start' => time() ); $p = $pages->add( $options['template'], $options['parent'], $options ); $p->of(false); // repeater_matrix_content - type: intro $intro = $p->repeater_matrix_content->getNew(); $intro->repeater_matrix_type = 1; // intro $intro->intro = 'Lorem ipsum'; $p->save('repeater_matrix_content'); // repeater_matrix_content - type: text $text = $p->repeater_matrix_content->getNew(); $text->repeater_matrix_type = 2; // text $text->body = 'Lorem ipsum dolor sit amet'; $p->save('repeater_matrix_content'); $videos = $p->repeater_matrix_content->getNew(); $videos->repeater_matrix_type = 4; // videos $videos->save(); foreach($urls as $url) { if (empty($url)) continue; $repeater_video = $videos->repeater_videos->getNew(); $repeater_video->link_url = $url; $videos->save('repeater_videos'); } $p->save('repeater_matrix_content'); The code to update the just added page. (not working). Note that the code for adding the repeater matrix items are identical to the above code. The first two items of the type (1=intro, 2=text) are succesfully added and saved. The problem occurs when trying to add repeater items to the repeater matrix type 3=video. What am i doing wrong? $options = array( 'template' => 'article', 'parent' => $parent = $pages->get('template=articles'), 'title' => 'This is a test', 'date_pub_start' => time() ); /** * Second run, updates existing article page * */ $id = $p->id; unset($p); $p = $pages->get($id); $p->of(false); $p->title = $options['title']; $p->date_pub_start = time(); $p->save(); foreach($p->repeater_matrix_content as $rp) { $pages->delete($rp, true); } // repeater_matrix_content - type: intro $intro = $p->repeater_matrix_content->getNew(); $intro->repeater_matrix_type = 1; // intro $intro->intro = 'Lorem ipsum'; $p->save('repeater_matrix_content'); // repeater_matrix_content - type: text $text = $p->repeater_matrix_content->getNew(); $text->repeater_matrix_type = 2; // text $text->body = 'Lorem ipsum dolor sit amet'; $p->save('repeater_matrix_content'); $videos = $p->repeater_matrix_content->getNew(); $videos->repeater_matrix_type = 4; // videos $videos->save(); foreach($urls as $url) { if (empty($url)) continue; $repeater_video = $videos->repeater_videos->getNew(); $repeater_video->link_url = $url; $videos->save('repeater_videos'); } $p->save('repeater_matrix_content'); Link to comment Share on other sites More sharing options...
Raymond Geerts Posted February 14, 2017 Author Share Posted February 14, 2017 I solved the update issue as follow: - before touching the repeater matrix field, count the already existing (n) repeater matrix items. - add all the new (updated) repeater matrix items, including the repeater items inside the repeater matrix. - remove the first (n) repeater matrix items from the field. Link to comment Share on other sites More sharing options...
elabx Posted July 31, 2017 Share Posted July 31, 2017 Hi! Any info on how to add repeater item types through API? Link to comment Share on other sites More sharing options...
adrian Posted July 31, 2017 Share Posted July 31, 2017 Take a look at this: https://github.com/adrianbj/ProcessAdminActions/blob/master/actions/PageTableToRepeaterMatrix.action.php It's purpose is to convert a PageTable to RepeaterMatrix, but you should find the code snippet in there that you need. 1 Link to comment Share on other sites More sharing options...
elabx Posted August 4, 2017 Share Posted August 4, 2017 @adrian Got this almost settled, do you have any information on how to save the fieldgroup conext settings for the repeater matrix field?? I see that they are saved through namespaces but I just can't seem to find a way to save them, this is what I am trying: EDIT: Found out, edited code below: $matrix_field_name = "content"; $matrix_type_namespace = "matrix12"; //Field inside repeater you want to edit it's property. $matrix_repeater_field_name = "feature_card_items"; $matrix_template = wire('templates')->get("repeater_$matrix_field_name"); $fieldId = wire("fields")->get("feature_card_items")->id; $matrix_fieldgroup = $matrix_template->fieldgroup; //Get the config data from the fieldgroup and the namespace which determines the field's settings based on the matrix repeater type. $matrix_field_data = $matrix_fieldgroup->getFieldContextArray($fieldId, $matrix_type_namespace); //Copy the data to edit it $matrix_field_data_new = $matrix_field_data; $matrix_field_data_new["description"] = "Bla bla bla"; //Reassign the data to the fieldgroup $matrix_fieldgroup->setFieldContextArray($fieldId, $matrix_field_data_new, $matrix_type_namespace); //Save fieldgroup context data, only unknwon wire("fields")->saveFieldGroupContext($matrix_fieldgroup->getField("feature_card_items", true), $matrix_fieldgroup); 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