Jump to content

find does not work as expected with autocomplete page reference field


torf
 Share

Recommended Posts

Hi everybody,

I tried to make only special pages accessible through a page reference field, but got a strange problem. In my ready.php I added

$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
	if($event->object->hasField == "choose_page") {
		$event->return = $event->pages->find("template=template_name_to_choose_from");
	}
});

But I keep getting all pages of my site back when I use autocomplete for my field. With select or radio it works as expected.
Can anybody tell me why?

Link to comment
Share on other sites

Oh. I didn't know this one. I just tried

$wire->addHookAfter('InputfieldPageAutocomple::findPagesSelector', function($event) {
	if($event->object->hasField == "choose_page") {
		$event->return = $event->pages->find("template=template_name_to_choose_from");
	}
});

but to no avail. I still get every page returned. Or maybe I have misunderstood how to use it.
What puzzles me is that it works perfectly with Dropdwon or Select. How is Autocomplete different? 

Link to comment
Share on other sites

On 3/11/2022 at 4:41 AM, torf said:

But I keep getting all pages of my site back when I use autocomplete for my field. With select or radio it works as expected.

The "Multiple page selection" version of Page Auto Complete doesn't support the "Custom PHP code" option (i.e. the InputfieldPage::getSelectablePages hook). In earlier versions of PW it used to be more explicit about that. Now it just removes the "Custom PHP code" option depending on what you have selected in "Input field type". Page List Select and Page List Select Multiple also do not support the "Custom PHP code" option.

2022-03-14_095612.thumb.png.ab8f538fcc6bc48e4478fd2f45f23cf8.png

2022-03-14_095657.thumb.png.b7a32e71829efbd03e89c6394852de5e.png

 

On 3/11/2022 at 4:41 AM, torf said:
$event->return = $event->pages->find("template=template_name_to_choose_from");

If you really are returning something like that from you hook then you could change to the "Selector string" option, which will work with autocomplete.

2022-03-14_100653.png.3cb911f89c9838a3a4e94c2b0c3cdb22.png

  • Like 2
Link to comment
Share on other sites

I see. Thanks for the clarification @Robin S I use a German backend translation and it seems the filter for showing only valid options does not show up in German...

1023083910_Screenshot2022-03-14052120.jpg.8a1c1cc79130ee718a8104c524258e6c.jpg

Sadly the selector is a bit more complicated in reality. Pages are chosen based on a value in another page selector field they hold. So the original selection is

$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
	if($event->object->hasField == "choose_page") {
		$page = $event->arguments(0);
		$zv = $page->parent->title;
		$event->return = $event->pages->find("template=my_Template, field_in_my_Template=$zv");
	}
});

I do not think that can be achieved without php.

Link to comment
Share on other sites

2 hours ago, torf said:

I do not think that can be achieved without php.

It might work as a selector string:

template=my_Template, field_in_my_Template=page.parent.title

Or if field_in_my_Template is a Page Reference field:

template=my_Template, field_in_my_Template=page.parent

 

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