alexcapes Posted December 9, 2015 Share Posted December 9, 2015 I have a simple module that creates a page when another page is saved with a checkbox field checked. My code below works well but I have one issue, it runs when the page is trashed as well as saved (creating an erroneous second duplication of the page). <? public function init() { $this->pages->addHookAfter('save', $this, 'dupeStandalone'); } public function dupeStandalone($event) { $page = $event->arguments[0]; if($page->template->name == "article_language" && $page->article_standalone == 1) { $a = new Page(); $a->template = 'article_standalone'; $a->parent = wire('pages')->get('/article/'); $a->name = $page->name; $a->title = "{$page->title} (standalone placeholder post for {$page->parent->parent->title}-only article)"; $a->save(); } } Could anyone help as to why it runs the function on trash as well as save? Link to comment Share on other sites More sharing options...
LostKobrakai Posted December 9, 2015 Share Posted December 9, 2015 if(!$page->isTrash()){…} A page, which should be trashed, is moved to another parent, which in turn needs to be saved. That's actually intended behaviour. 1 Link to comment Share on other sites More sharing options...
alexcapes Posted December 9, 2015 Author Share Posted December 9, 2015 if(!$page->isTrash()){…} A page, which should be trashed, is moved to another parent, which in turn needs to be saved. That's actually intended behaviour. Ahh hah! Lighbulb moment, thanks @LostKobrakai! I also located a bit of info in an old post from Ryan... "Both the trash() and restore() functions are triggered by $pages->save() when it detects that the parent has changed to or from the trash" In that post he says you can use this to check if a page has moved... if($page->parentPrevious Also checking if it is in trash makes sense too. 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