Jump to content

Tip: How to get calling page with page autocomplete inputfield


Kiwi Chris
 Share

Recommended Posts

I've been having a bit of a struggle converting a page reference field from Select to Autocomplete, as with Select, it's possible to hook getSelectablePages, and the actual page the inputfield is on is available to the hook, but with autocomplete, you have to hook findReady, and the page the field is on is not passed to the hook, only the name of the field, which then looks up the selector string from the field definition.

None of the hook arguments include the calling page, however $event->argument(0) gives the selector string, and in the field definition it is possible to include some page fields in the selector string in the field definition. eg:

The problem is, typically these will affect the pages returned, but I realised that if I added at the START of the selector string in the field definition id!=page.id that will have no influence on the pages returned, as I'm not likely to want a circular reference with a page referring to itself in a page reference field, but this will provide the actual calling page id that requested the page autocomplete.

Since the selector string is just a comma delimited list, it's easy enough to convert this to an array, and if I have id!=page.id as the FIRST selector parameter it's easy to get it like so:

 

$selector = $event->arguments(0);
$selectors = explode(',', $selector);
$pageSelector = explode('=', $selectors[0]);
$page = wire('pages')->get($pageSelector[1]);

Now that I have the actual page that the autocomplete is on, I can do things beyond what is possible with the selector string in the field definition.

In my case I wanted to do this:

$owner = $page->parent('template=client'); //Get ancestor page with given template and retrieve a field value from it to use in selector

I can then rewrite the selector string as desired.

I suppose in theory it would be possible to not use the selector string in the field definition as a selector string at all, but rather as a way of passing parameters to the hook, and then just build your own selector string in the hook.

I hope this helps others who've struggled with figuring out where ProcessPageSearch::findReady is being called from. 

It won't necessarily work for all situations but should work when called from page autocomplete.

Edit: I discovered that ProcessWire adds other selectors after the last selector in the selector string in the field definition, so it's really important that id!=page.id is the FIRST parameter in the selector string in the field defintion, unless you like searching arrays. If you make it first, you just need to explode the search string, get the first array element, explode that again, and you've got the page id.

Edited by Kiwi Chris
Realised that putting the id last doesn't work, as ProcessWire adds other selectors after it. It has to be first.
  • Like 1
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

×
×
  • Create New...