Jump to content

Get selectable pages of page reference field without template context?


MoritzLost
 Share

Recommended Posts

I want to display all options (i.e. all selectable pages) of a page reference field in the frontend. Normally I would access the available options through the fieldgroup of the page's template like this:

$options = $page->template->fieldgroup
	->getField('my_page_ref_field')
	->getInputfield($page)
	->getSelectablePages($page);

However, now I need to get the options of a field which is part of a repeater, so it doesn't exist in the current page's fieldgroup. I managed to get the selectable pages through the repeater field's template:

$options = $templates->get('repeater_with_my_field')
    ->fieldgroup
    ->getField('my_page_ref_field')
    ->getInputfield($page)
    ->getSelectablePages($page);

However, I don't like this solution. It depends on the field being part of the specified repeater field, which might change in the future. Also, I'm passing in $page which isn't really correct since the $page doesn't have that field. Not sure if this will cause problems, but it feels wrong.

Is there a better way to get the selectable pages without the context of a page / template / fieldgroup?

Link to comment
Share on other sites

10 hours ago, MoritzLost said:

Is there a better way to get the selectable pages without the context of a page / template / fieldgroup?

You could get the field without going via the template fieldgroup, but to get the inputfield and selectable pages I think it's compulsory to supply a Page object. In the case of selectable pages, it's quite common for the selectable pages to change depending on the current page which is perhaps why it's a compulsory argument to getSelectablePages().

I guess you could get the inputfield settings that determine the selectable pages (i.e. parent_id, template_id, findPagesCode, findPagesSelect or findPagesSelector) and then use that to build a $pages->find() selector but that seems like more trouble than supplying a $page argument.

For a Page Reference field that isn't in a repeater I think the simplest way is:

$options = $page->getInputfield('my_page_ref_field')->getSelectablePages($page);

But a Page Reference field inside a repeater is a special case because where the selectable pages depend on the current page, the current page is interpreted as the editable page that contains the repeater, not the repeater page that contains the inputfield. There is discussion about this here: https://github.com/processwire/processwire-issues/issues/479

So you might do something like this:

$options = $fields->get('my_page_ref_field')->getInputfield(new Page())->getSelectablePages($page);

 

  • 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...