Jump to content
ttttim

Page reference field exclude items already used in different field

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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By JeevanisM
      Hello All,
      Recently I have read a series of articles about some new terms like Headless CMS, Hybrid CMS and Digital Experience Platforms ( DXP). I cannot get a clear picture of these but I could understand that these are different from the traditional CMS as we use now, at least in theoretical level. Does any one here have any better understanding on these topics ? may be what / how we can apply these on the processwire CMS ?
       
      just for the talk
       
    • By Manuel
      Hi everybody!
      I'm new to PW and just did the beginner-tutorial “Hello Worlds”, a beginning ProcessWire tutorial --> Link
      After extending the template from the original tutorial with a page_reference-Field (Page-Field value type is configured as single page),
      PW throws a fatal error when i try to output multiple pages:
      $planets = $pages->find("template=planet, sort=-title"); foreach($planets as $planet) { echo $planet->title; echo $planet->planet_reference->title; } Should return three Pages, but throws the following error:
      When i try to output the field with a single page, everything just works fine:
      $planet = $pages->get(1018); echo $planet->title; echo $planet->planet_reference->title; I've tried to solve the problem with myself (and google), but i can't fix the error.
      Thanks a lot!

      My enviroment:
      OSX Mojave / MAMP (PHP 7.2), PW v 3.0.123
    • By MischaK
      I'm someone who has been developing websites for a while, mostly doing full stack .NET development and ASP scripting before that, while using Silverstripe and some other PHP frameworks for some smaller projects which worked well enough for quite some time. 
      Now I have some time on my hands and decided to evaluate some different CMS options. 
      Diving in as a newbie into: 
      Wordpress 
      Processwire 
      CraftCMS
      After a short while I decided that Wordpress was out while I'm still evaluating Craft and Processwire. 
      My question is, is there any interest here in reading about my experiences in trying to get the feature set I want up and running, with these other systems?
    • By dragan
      Is it somehow possible to limit the number of pages one can select with the page reference field?
      I have a case where I need to have exactly three, not more, not less. Is this doable with a hook? Or would you suggest another approach? (three single pageref fields and make each mandatory)
    • By DaveDev
      I want the admin to select one of the child of a parent in "page reference" as radio buttons, but it comes out empty
      here is my hook in site/ready.php
      $wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {   if ($event->object->hasField == 'childLinks') {     $page = $event->arguments('page');     $parent = "parent=" . $page->path;     $event->return = $event->pages->find($parent);   } });  
×
×
  • Create New...