Jump to content

[Solved] Inputfield::getSelectablePages After hook seems to ignore $event->return value in nested repeaters


iank
 Share

Recommended Posts

Hi,

I'm not sure if anyone can help with this, and it may just be a limitation of the repeater nesting situation I've found myself in, but here's the background:

I have a site built some time ago (and I'm sure this used to work, but I've since upgraded Processwire).  It has a structure that guides the visitor through a series of questions to an ultimate end point, and is structured something like this:

  • Plants (page, template = ndf)
    • Step 1 (page, template = step)
      • Question 1.1  (page, template = substep)
        • Answer (field, repeater matrix) [used because the answer isn't always Yes/No, but there are alternative answer types]
          • Yes/No Answer (field, repeater)
            • Yes (repeater item)
              • Next step (page reference field (ref_step), referencing multiple substeps)  [should only show substeps inside parent Plants]
            • No (repeater item)
              • Next step (ref_step = page reference field (ref_step), referencing multiple substeps) [should only show substeps inside parent Plants]
    • Step 2
    • Step 3 etc..
  • Timber (page,template =ndf)
    • Step 1
    • Step 2
    • Step 3 etc

When a substep page is displayed, it allows the visitor to choose Yes or No, and they will then be redirected to the next step (another substep page). These are different substeps for Yes and No. 

The answers Yes and No are (standard) repeater items, with various bits of content (summary text etc), but have a Page reference field that should allow the selection of the next step, (which is another substep) in the admin. 

This all works, but the choice of substeps should be limited to those inside their ultimate (grand)parent (ndf template page - just those inside Plants, or inside Timber respectively).

Hence I have an 'After' hook on Inputfield::getSelectablePages where I identify the parent ndf page (Plants or Timber) and select the corresponding substep pages and allocate these to $event->return.

The selection of the correct pages is working (I can see this from a Tracy dump), but this is never returned to the field instance, and instead it just uses the default settings for the field, which is based on template=substep and is returning all substep pages for all (grand)parents.

Code for the hook (in ready.php):

$wire->addHookAfter('InputfieldPage::getSelectablePages', function (HookEvent $event) {
  $page = $event->arguments('page');
  $fieldName = $event->object->name;
  if ($fieldName == 'refStep') {
    $ndf = $page->getForPage()->getForPage()->parents("template=ndf")->first(); //move up the tree to get the correct ndf grandparent
    //get the steps only for the current ndf
    $selector = "template=substep,has_parent=$ndf";
    $steps = $event->pages->find($selector);  //<== contains the correct PageArray, verified in Tracy bd
    $event->return = $steps; //<==doesn't seem to do anything.
  }
});

Sorry that's so complex, but I'm a bit stuck.  I've used Inputfield::getSelectablePages many times before without issues, and as I mentioned, I feel sure this used to work.  I may have to revert my Processwire upgrade to prove that however ?

Any thoughts are welcome!

Thanks,
Ian

Link to comment
Share on other sites

7 hours ago, iank said:
$fieldName = $event->object->name;

This gets the inputfield name, not the field name. The inputfield name will have a suffix when it appears inside a repeater, so checking it against a particular field name is not a reliable way to identify the field.

The settings for a Page Reference field gives you an example hook for when you want to use the "Custom PHP code" option.

image.png.2f2f977ab11f12a4679d9bff45742f30.png

The example shows that the best way to identify the field is via the hasField property. So for your hook:

$wire->addHookAfter('InputfieldPage::getSelectablePages', function (HookEvent $event) {
	$page = $event->arguments('page');
	if($event->object->hasField == 'refStep') { 
		// Your hook code here...
	}
});

 

  • Thanks 1
Link to comment
Share on other sites

That's great, thanks @Robin S!  Works perfectly.  

A bit stupid of me - looking back at my old code I did use hasField, but evidently changed it during some other troubleshooting and didn't realise/forgot to change it back ?.

As usual, the problem was entirely mine, and when in doubt, read the instructions!!

 

  • Like 1
Link to comment
Share on other sites

  • iank changed the title to [Solved] Inputfield::getSelectablePages After hook seems to ignore $event->return value in nested repeaters

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...