LMD Posted July 10, 2023 Share Posted July 10, 2023 I'm trying to use a conditional "saveReady" hook for repeater items inside a module, but for some reason it isn't firing (and yes, I am making sure to make changes to the repeater when testing!). It works when NOT using a conditional hook (and checking for the template inside the hook function itself), so it's not an issue with anything except the conditional part of the hook. // THIS WORKS public function ready() { $this->addHookAfter('Pages::saveReady', $this, 'myHook'); } public function myHook(HookEvent $event) { $page = $event->arguments(0); if ($page->template->name === 'repeater_gallery') { bd($page); // Tracy dump } } // THIS DOES NOT WORK! public function ready() { $this->addHookAfter('Pages(template=repeater_gallery)::saveReady', $this, 'myHook'); } public function myHook(HookEvent $event) { $page = $event->arguments(0); bd($page); // Tracy dump -- does not get dumped. Is the hook not fired? } I'm using the latest DEV version of ProcessWire (v3.0.222). Is this a bug, or is it me? Link to comment Share on other sites More sharing options...
BitPoet Posted July 10, 2023 Share Posted July 10, 2023 28 minutes ago, LMD said: Is this a bug, or is it me? It's expected behavior as the selector in a conditional hook is applied to the event's object, which in this case is $pages, not the page that is about to be saved. You can convince PW to match the selector against the hook's arguments by stating the argument's number in front of the selector (there's an example in the docs towards the end of the conditional hooks section). In your case for the first argument, which contains the page: // match selector against $event->arguments(0) instead of $event->object: $this->addHookAfter('Pages(0:template=repeater_gallery)::saveReady', $this, 'myHook'); 2 Link to comment Share on other sites More sharing options...
ryan Posted July 10, 2023 Share Posted July 10, 2023 @LMD I think maybe this is what you want? This is saying to match template=repeater_gallery on the first argument to the method: $wire->addHook('Pages::saveReady(template=repeater_gallery)', function($event) { $page = $event->arguments(0); $event->message("Saving page with template=$page->template"); }); 3 Link to comment Share on other sites More sharing options...
LMD Posted July 10, 2023 Author Share Posted July 10, 2023 Ahh, yes, I was not taking into account 'Pages' vs 'Page' objects. Which is silly of me, because the clue is right there in the "$page = $event->arguments(0)" bit of the hook method! Thank you! 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