Jump to content

Recommended Posts

Posted

In 2015, Ryan introduced conditional hooks:
https://processwire.com/blog/posts/new-ajax-driven-inputs-conditional-hooks-template-family-settings-and-more/

I have the following hook which works properly:

$wire->addHookAfter('Pages::added', function($event) {
  $page = $event->arguments[0];
  if($page->template=='mytemplate') {
    // ...
  }
});

Now if I want to take advantage of conditional hooks, I should be able to do this:

$wire->addHookAfter('Pages(template=mytemplate)::added', function($event) {
  // ...
});

however the hook doesn't get hooked.  Any ideas?

Posted

So the conditional hooks (probably) work on the $event->object of the HookEvent passed to the hook. That's why ryan's examples only use Page(…)::…. Page does not have a function added (neither hookable nor unhookable), while Pages cannot be evaluated against a selector. 

Posted

I've also been struggling with this:

Cr6i6CA.png

Thats because the $page you want to check is the first argument of the HookEvent, so you need to apply it there, not at the first part, which is the class (or object). You can also do this:

4UNRyy2.png

  • Like 4
  • 4 years later...

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