Jump to content

How to assign custom pageArray to each instance of a repeater


Sanyaissues
 Share

Recommended Posts

Hi, I have a page with a repeater called teams with two fields: team (a pageReference with a list of teams) and people (a pageReference with a list of people who likes this team).

I'm trying to populate the people field in the repeater according to the value of the team field in the same repeater. So far. I'm able to get the pageArray that I want to assign to the people field, but i don't know how to "save" the value for each instance of the repeater.

I hope somebody can give me a light. Thanks in advance.

 

$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
  if($event->object->hasField == 'people') { 
    $repeaterField = $event->arguments('page')->teams; 
    foreach ($repeaterField as $t) { // Is this the way to loop the instances of the repeater?
      $team = $t->team->id;
      $t->people = $event->pages->find("template=user, team={$team}");
      var_dump($t->people); // This returns the values that i want to assign for each repeate instance
      $event->return = $t->people; // I hope this assign a custom pageArray for each repeater, but it assigns the same for all the instances of the repeater
    }
  }
});

 

Link to comment
Share on other sites

In the getSelectablePages() hook $event->arguments('page') is the page being edited, but that is not so useful in your case because what you want is the Repeater page that contains the two Page Reference fields.

Give this a try:

$wire->addHookAfter('InputfieldPage::getSelectablePages', function(HookEvent $event) {
    if($event->object->hasField == 'people') {
        $inputfield = $event->object;
        $repeater_page = $inputfield->hasPage;
        if($repeater_page->team && $repeater_page->team->id) {
            $event->return = $event->pages->find("template=user, team=$repeater_page->team");
        }
    }
});

Note that if the user changes the selection for the team field they will need to save the page before the selectable options for the people field will update.

Edited by Robin S
Added conditional for field name
  • Like 3
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...