Jump to content

Page Reference Autocomplete returning all pages


torf
 Share

Recommended Posts

I have a page reference field that only shows certain pages based on a field on the the chosen page that also exists at the parent page.  To get the allowed pages I added in ready.php

$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
	if ($event->object->hasField == "my_page_reference_field") {
		$page = $event->arguments(0);
		$zv = $page->parent->name;
		$event->return = $event->pages->find("template=myTeplate, searchterm=$zv");
	}
});

As long as I use radiobuttons or select field this works great. But using automplete I get all the pages from the whole site.

Does anybody know why this happens?

Link to comment
Share on other sites

This is because Page Autocomplete doesn't use InputfieldPage::getSelectablePages. Instead, it sends a live query to ProcessPageSearch. Unfortunately, this means that the field settings are written to the JS config, and support for selectors is somewhat limited there.

There's been a github issue around for some time with a workaround, which unfortunately means adding a few lines to a core js file, so it needs to be reapplied after every update: https://github.com/processwire/processwire-issues/issues/1374#issuecomment-835032978

  • Like 4
Link to comment
Share on other sites

On 4/27/2023 at 8:04 PM, torf said:

To get the allowed pages I added in ready.php

$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
	if ($event->object->hasField == "my_page_reference_field") {
		$page = $event->arguments(0);
		$zv = $page->parent->name;
		$event->return = $event->pages->find("template=myTeplate, searchterm=$zv");
	}
});

You can actually adapt that to a selector string that will allow you to use Page Autocomplete:

image.png.42b18cf55ab221e46151b7b826fa3ac4.png

In a Page Reference field selector string, "page" is a special keyword that PW converts into "the page that is open in Page Edit". This isn't documented anywhere as far as I know, but it would be good if it was @ryan.

  • Like 3
Link to comment
Share on other sites

Wow. Cool one. It absolutely works. I cannot use it in my special case as I have to use ready.php (the field is used in two templates and only one uses the parent as selector. So I need to use an if statement to determine the actual template), but it's good to know it works that way as well.

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...