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?