a-ok Posted July 4, 2019 Share Posted July 4, 2019 I'm attempting to change the page name of a page, when it is saved, using the API. Below is my hook. The message appears (as a test) but the change never happens. Any thoughts? function myHook(HookEvent $event) { $page = $event->arguments(0); if ($page->template->name == 'work-detail') { if ($page->global_text) { $pageName = wire()->sanitizer->pageName($page->title . '-' . $page->global_text, true); } else { $pageName = wire()->sanitizer->pageName($page->title, true); } wire()->message($pageName); $page->setOutputFormatting(false); $page->setAndSave('name', $pageName); } } wire()->addHookAfter('Pages::save', null, 'myHook'); // On page SAVE run this hook Link to comment Share on other sites More sharing options...
bernhard Posted July 4, 2019 Share Posted July 4, 2019 I don't know it right now but maybe the page name is reset after that hook. Does it work using Pages::saved instead of ::save? I'm usually using saveReady or saved to hook ? Link to comment Share on other sites More sharing options...
a-ok Posted July 4, 2019 Author Share Posted July 4, 2019 4 minutes ago, bernhard said: I don't know it right now but maybe the page name is reset after that hook. Does it work using Pages::saved instead of ::save? I'm usually using saveReady or saved to hook ? Thanks for the reply! It does not, sadly. Grrrr. Link to comment Share on other sites More sharing options...
a-ok Posted July 4, 2019 Author Share Posted July 4, 2019 Bit of an update. Using TraceyDebugger it looks as if the name does change (so to speak), it even says so in the session, but it isn't updated on the backend or the frontend. Link to comment Share on other sites More sharing options...
dragan Posted July 4, 2019 Share Posted July 4, 2019 Try this if ($this->page->template == 'admin') { function customPageName(HookEvent $event) { $dummy = 'hook-generated'; $page = $event->arguments(0); if ($page->template->name == 'basic-page') { $pageName = wire()->sanitizer->pageName($page->title . '-' . $dummy, true); $page->setOutputFormatting(false); $page->name = $pageName; } } $this->addHookAfter('Pages::saveReady', null, 'customPageName'); }; I guess a certain combination of hook + save() will create an infinite loop, but this (abbreviated) example works just fine here. Link to comment Share on other sites More sharing options...
a-ok Posted July 4, 2019 Author Share Posted July 4, 2019 10 minutes ago, dragan said: Try this if ($this->page->template == 'admin') { function customPageName(HookEvent $event) { $dummy = 'hook-generated'; $page = $event->arguments(0); if ($page->template->name == 'basic-page') { $pageName = wire()->sanitizer->pageName($page->title . '-' . $dummy, true); $page->setOutputFormatting(false); $page->name = $pageName; } } $this->addHookAfter('Pages::saveReady', null, 'customPageName'); }; I guess a certain combination of hook + save() will create an infinite loop, but this (abbreviated) example works just fine here. Many thanks for this. I haven't tried it yet but what's the fundamental difference here? The "$page->name =" rather than "setAndSave"? Link to comment Share on other sites More sharing options...
dragan Posted July 4, 2019 Share Posted July 4, 2019 I have followed @bernhard's suggestion and hooked into saveReady, and replaced setAndSave() with just $page->name = $pageName. Because... a hook named saveReady doesn't need a separate (injected) save() command. You can SET values right before it SAVES things. 2 Link to comment Share on other sites More sharing options...
a-ok Posted July 4, 2019 Author Share Posted July 4, 2019 5 minutes ago, dragan said: I have followed @bernhard's suggestion and hooked into saveReady, and replaced setAndSave() with just $page->name = $pageName. Because... a hook named saveReady doesn't need a separate (injected) save() command. You can SET values right before it SAVES things. Brilliant. That does work. Why would the first code work, but not the second? if ($this->page->template == 'admin') { function customPageName(HookEvent $event) { $dummy = 'hook-generated'; $page = $event->arguments(0); if ($page->template->name == 'work-detail') { if ($page->global_text) { $pageName = wire()->sanitizer->pageName($page->title . '-' . $page->global_text, true); } else { $pageName = wire()->sanitizer->pageName($page->title, true); } $page->setOutputFormatting(false); $page->name = $pageName; } } $this->addHookAfter('Pages::saveReady', null, 'customPageName'); }; function myHook(HookEvent $event) { $page = $event->arguments(0); if ($page->template->name == 'work-detail') { if ($page->global_text) { $pageName = wire()->sanitizer->pageName($page->title . '-' . $page->global_text, true); } else { $pageName = wire()->sanitizer->pageName($page->title, true); } $page->setOutputFormatting(false); $page->name = $pageName; } } wire()->addHookAfter('Pages::saveReady', null, 'myHook'); Link to comment Share on other sites More sharing options...
dragan Posted July 4, 2019 Share Posted July 4, 2019 try changing 20 minutes ago, a-ok said: wire()->addHookAfter('Pages::saveReady', null, 'myHook'); to 20 minutes ago, a-ok said: $this->addHookAfter('Pages::saveReady', null, 'customPageName'); Link to comment Share on other sites More sharing options...
a-ok Posted July 4, 2019 Author Share Posted July 4, 2019 Now I'm confused. That didn't work but that made it almost 100% like your example. I commented mine out and put back yours and it worked. Bizarre. I even put wire()-> back in instead of $this and it still worked – maybe a hidden glyph or something? Link to comment Share on other sites More sharing options...
dragan Posted July 4, 2019 Share Posted July 4, 2019 12 minutes ago, a-ok said: maybe a hidden glyph or something? yeah, since a recent forum update, copy-and-pasting code snippets can wield weird results ? 1 Link to comment Share on other sites More sharing options...
a-ok Posted July 4, 2019 Author Share Posted July 4, 2019 44 minutes ago, dragan said: yeah, since a recent forum update, copy-and-pasting code snippets can wield weird results ? Ahh. Your example was fine though; it was my original that wasn't. Who knows. Hate that! Thanks for all the help, you too @bernhard 2 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