Skip to content

Example "Custom PHP code" for Page Reference field fails inside repeater #267

@Toutouwai

Description

@Toutouwai

Short description of the issue

When using the "Custom PHP code" option to limit selectable pages for a Page Reference field, the example code given is:

$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
  if($event->object->name == 'my_page_field') {
    $event->return = $event->pages->find('your selector here');
  }
});

But the if() condition will fail when the field is contained within a repeater item because in that case the inputfield name will be my_page_field_repeater1234. Users may not know how to debug this issue and it would be better if the example code worked for all contexts where a Page Reference field may be used.

Related forum post: https://processwire.com/talk/topic/13677-selectable-pages-for-page-field-inside-a-repeater-inside-a-repeatermatrix/?do=findComment&comment=145194

Optional: Suggestion for a possible fix

While the inputfield name changes when used inside a repeater, the name of the field associated with the inputfield does not change. So perhaps the example code could be changed to:

$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
  if($event->object->hasField->name == 'my_page_field') {
    $event->return = $event->pages->find('your selector here');
  }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions