torf Posted April 27, 2023 Posted April 27, 2023 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?
BitPoet Posted April 28, 2023 Posted April 28, 2023 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 4
torf Posted April 28, 2023 Author Posted April 28, 2023 Thanks a lot. In that case I just use the normal select. Changing core files only leads to trouble and I try to avoid it if not absolutely necessary. 1
Robin S Posted April 28, 2023 Posted April 28, 2023 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: 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. 3
torf Posted April 29, 2023 Author Posted April 29, 2023 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now