Jump to content

Hook into Selector


kongondo
 Share

Recommended Posts

Hi,

I am trying to hook into page finder to insert a field=my-value that should always be added to all page finds/gets. my-value is generated dynamically hence I cannot hardcode this. I have searched and cannot seem to find anything in the forums. I have looked at DynamicRoles for inspiration but didn't find anything clear. I have seen PageFinder::getQuery and PageFinder::getQueryAllowedTemplatesWhere but not sure how to use any of these. For instance, if I needed to add a has_parent=1234 to all find/get/findRaw/getRaw selectors, how would I do it?

ps: This is not about roles and permissions per-se. Just an easy way to constrain finder at the db level.

Thanks!

Link to comment
Share on other sites

12 hours ago, elabx said:

Wouldn't hooking into PageFinder::find() work?

Thank you sir! Seems to work fine as below:

<?php

namespace ProcessWire;

// site/ready.php

// HOOK
$this->addHookBefore('PageFinder::find', null, 'hookBeforePageFinderFind');


function hookBeforePageFinderFind(HookEvent $event)
{  
  $selectors = $event->arguments(0);
  // AMEND SELECTOR
  $selectors->init("has_parent={$myDynamicID}");
  // Populate back arguments (if you have modified them)
  // DON'T THINK THIS IS NEEDED AS IT STILL WORKS!?
  $event->arguments(0, $selectors);
}

Will do more tests and report here if I come across issues.

Thanks!

  • 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

  • Recently Browsing   0 members

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