Jonathan Lahijani Posted November 15, 2018 Share Posted November 15, 2018 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? Link to comment Share on other sites More sharing options...
Jonathan Lahijani Posted November 15, 2018 Author Share Posted November 15, 2018 Note: whether I use Page(template=mytemplate)::added or Pages(template=mytemplate)::added does not make a difference. Link to comment Share on other sites More sharing options...
LostKobrakai Posted November 15, 2018 Share Posted November 15, 2018 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. Link to comment Share on other sites More sharing options...
bernhard Posted November 15, 2018 Share Posted November 15, 2018 I've also been struggling with this: 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: 4 Link to comment Share on other sites More sharing options...
LAPS Posted January 4, 2023 Share Posted January 4, 2023 @bernhard So, the advice is not to use conditional hooks if not as made in @ryan's example(s)? Link to comment Share on other sites More sharing options...
bernhard Posted January 4, 2023 Share Posted January 4, 2023 No, conditional hooks are great. I just tried to show that this: Pages(template=foo)::saved is wrong and this is the correct version: Pages::saved(template=foo) See https://processwire.com/talk/topic/18037-2-date-fields-how-to-ensure-the-second-date-is-higher/#comment-158164 and 4 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now