Jump to content

Getting one repeater items as content for other repeater item on the same page?


theoretic
 Share

Recommended Posts

Hello there! And thanks for Processwire.

Got one more interesting question. One of my templates called goods has two repeaters. The first one is variants, each item containing possible sizes, colors and so on. The second one is available_variants, each item should contain a region and a variant which should be selectable from the list of variants.

So the task is to get items of one repeater from inside the page reference field which is the part of another repeater, both repeater belonging to the same page.

Processwire gives some options for retrieving selectable pages in this case. We can use:

  • Parent. Not an option because we need dynamic parent being in fact the current page which contains both repeaters.
  • Template. Not an option again because we need to select repeater items not only by template but also by page id which is dynamic.
  • Custom find. Could be a solution but have no idea how to substitute the current page id into ID query.
  • Selector string. It's almost what we need, but again i have no idea how to substitute something like "parent={$parent->id}" in this case.
  • Custom PHP code. This needs to be placed inside site/ready.php and should contain a hook which finds pages using all the power of PW API.

Think that the last option is the solution, but could not get the result i need.

The simplest possible way seems to use $event->arguments('page')->variants. But it returns nothing. Tried different selectors like "template=repeater_variants, parent=".$event->arguments('page')->id , but still no result.

Will be grateful for any help. Thanks in advance!

Link to comment
Share on other sites

Try this in /site/ready.php...

$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
    if($event->object->hasField == 'your_page_reference_field_name') {
        $page = $event->arguments('page');
        if($page instanceof RepeaterPage) $page = $page->getForPage();
        $event->return = $page->variants;
    }
});

 

Edited by Robin S
Fix to get container page of Repeater item
  • Like 2
Link to comment
Share on other sites

10 minutes ago, theoretic said:

Tried this, but still no result, the select options are empty howerver $page->variants is nonempty.

Right - I forgot that your Page Reference field is inside a repeater, so that means in the hook $page is the repeater page and we need to get its container page. I edited my post above and it should work now.

  • 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

×
×
  • Create New...