Jump to content

Page reference field exclude items already used in different field


ttttim
 Share

Recommended Posts

Hi,

I'm working on a website where in the back-end in one template i have multiple page reference fields using the same conditions (parent and template). Is there a way if i selected a page in field A it won't show up anymore in the list of field B or field C etc?

I was looking at creating a custom string but i couldn't wrap my head around it.

Link to comment
Share on other sites

You can achieve this using the "Custom PHP code" option for the Page Reference fields, but your site editors will need to save the page after making a selection in each Page Reference field in order for the selected items to be removed from the options of the other fields.

Example hook in /site/ready.php:

$wire->addHookAfter('InputfieldPage::getSelectablePages', function(Hookevent $event) {
	$page = $event->arguments(0); // The page being edited
	if($event->object->hasField == 'colours') {
		// Exclude options selected in field colours_2
		$event->return = $event->pages->find("template=colour, id!={$page->colours_2}");
	}
	if($event->object->hasField == 'colours_2') {
		// Exclude options selected in field colours
		$event->return = $event->pages->find("template=colour, id!={$page->colours}");
	}
});

Result in Page Edit:

colours.gif.af77f154ee57db0aeb849dfea8b5eadc.gif

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