Jump to content

How to add repeater items to repeater inside repeater matrix


Raymond Geerts
 Share

Recommended Posts

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

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 by BitPoet
  • Like 3
Link to comment
Share on other sites

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

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

  • 5 months later...

@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);

 

  • Like 1
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...