Jump to content

Page Reference Field - selector for pages which have 0 references

Erik Richter

Recommended Posts

Hi everyone,

I am trying to establish a one-to-one relationship (Modules like ConnectPageFields won't work due to circular errors). My idea is, just having a page select field (single) on the user template, where I can select a page with a specific template.

When doing this for the next user, I want to make sure, that only those pages are availabe, which are not referenced from another user. Each page should only be selected once.

This might be possible with a custom selector in Page Reference Field configuration, tho I don't know how. I am aware of the page->references() method, but how to I get "pages with a specific template, that have references=0" as options? Or do I have to build a hook?

Thanks for help in advance!!

Link to comment
Share on other sites

For a field named "colour" allowing pages using a template named "colour":

$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
	$page = $event->arguments(0);
	$pages = $event->wire('pages');
	if($event->object->hasField == 'colour') {
		// Find pages that are already selected on another user page
		$already_selected = $pages->find("template=colour, colour.owner.template=user, colour.owner.id!=$page->id");
		// Selectable pages are those that are not already selected
		$event->return = $pages->find("template=colour, id!=$already_selected");

Owner selectors: https://processwire.com/blog/posts/processwire-3.0.95-core-updates/

Edited by Robin S
Forum glitch caused post duplication
Link to comment
Share on other sites

Hey Robin!

Just saw the answer - thank you! Just what I needed - perfect!

Can you also point me to the right hook  - after save or after field select-change of user, that fills a reference page on the "color" page with the user? Or just puts a checkbox ("linked to a user") to checked?


Just noticed that this is not working with autocomplete, only with the regular select option. Also found the github thread regarding this issue - someone knows of any progress regarding "custom php code and autocomplete page reference"?

Have a great weekend!

Edited by Erik Richter
Link to comment
Share on other sites

On 6/7/2020 at 10:41 AM, Erik Richter said:

Can you also point me to the right hook  - after save or after field select-change of user, that fills a reference page on the "color" page with the user?

That's the thing that isn't possible due to the circular reference issue with "single" Page Reference fields.

My suggestion: make one of your Page Reference fields a "multiple" field, even if you are only going to store a single page in it. That way you can use Connect Page Fields and your job will be a lot simpler. You won't need to use PHP to define the selectable pages so you'll be able to use the Page Autocomplete inputfield. Instead you can use a selector string to define the selectable pages and identify pages that not selected by another user with a string like "selected_by_user.count=0" where selected_by_user is a Page Reference field.

If your user pages are only managed by superuser then you don't need to do anything special with the inputfield - it won't matter if it technically allows multiple pages to be selected because you'll know to only select a single page. Or if others need to manage the users and you think they'll be confused if the inputfield allows multiple selections then you can limit the number of selectable pages with a hook:

$wire->addHookAfter('InputfieldPageAutocomplete::renderReadyHook', function(HookEvent $event) {
	/** @var InputfieldPageAutocomplete $inputfield */
	$inputfield = $event->object;
	$field = $inputfield->hasField;
	if($field && $field->name === 'colour') {
		$inputfield->maxSelectedItems = 1;

And of course the value of this field will be a PageArray but you can get the selected page by using the first() method.

  • 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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...