Jump to content

Problem with Page Autocomplete using findReady hook not saving data


Kiwi Chris
 Share

Recommended Posts

Not sure whether it's a bug, an inherent limitation of hooking findReady, or that I need to hook some other code as well, but I've found that while hooking findReady can allow me to modify the list of selectable pages for page Autocomplete inputfield for page fields without problem, on save, the data is not saved, and editing the page or reloading always comes up with blank data.

This is a scenario where I've provided a selector string in the input tab of the admin UI to configure the field, and then use the findReady hook to read data from this string and modify it to return the correct page list.

Not sure if anyone else has experienced this, or come up with a solution?

Link to comment
Share on other sites

1 hour ago, Kiwi Chris said:

This is a scenario where I've provided a selector string in the input tab of the admin UI to configure the field, and then use the findReady hook to read data from this string and modify it to return the correct page list.

I think the key thing will be to make sure the selector string you've defined in the field settings does not exclude any pages that you might want to include in the modified selector coming from your findReady hook. In other words, any selector in the settings should be broader than the selector from the hook. This is because any pages set to the inputfield will be validated against the field settings on save. I would make the field settings very broad (e.g. just a template) and then narrow this in your hook.

Link to comment
Share on other sites

21 hours ago, Robin S said:

I would make the field settings very broad (e.g. just a template) and then narrow this in your hook.

Thanks, that seemed to be the issue. I had included other criteria that I overrode in the hook.

For the record, this seemed to do the trick:

id!=page.id, template=stockItem

One of the annoyances with findReady is it doesn't give you the page that called it, but including id!=page.id is pretty broad and covers anything other than the calling page, which isn't usually going to reference itself. With this selector, I can pretty much do everything I could otherwise with getSelectablePages, so I can filter on any page criteria without needing to include them in the selector string. I was trying to have some parameters both in the selector string and override them.

 

Link to comment
Share on other sites

26 minutes ago, Kiwi Chris said:

One of the annoyances with findReady is it doesn't give you the page that called it

Since PW 3.0.238 you can do this:

$wire->addHookAfter('ProcessPageSearch::findReady', function(HookEvent $event) {
	/** @var ProcessPageSearch $pps */
	$pps = $event->object;
	$selector = $event->return;
	$for_selector_name = $event->wire()->input->get('for_selector_name');
	if($for_selector_name) {
		$data = $pps->getForSelector($for_selector_name, true);
		// $data includes the PageAutocomplete field name and the ID of the edited page

		// Do something with $selector...
	}
});

 

  • Like 2
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...