Jump to content

Page save() inside of a before SavePageField hook prevents from saving the field


Ivan Gretsky
 Share

Recommended Posts

Good day!

I have a hook

addHookBefore('FieldtypeMulti::savePageField'...

Inside that hook I need to  create and save an additional page.

If I add that code to the hook, on the original page save the changes to Page field do not get saved. All other fields do. If I change hook to After the problem goes away. But I have so much code now I do not know if it is safe to change hook to run after the method.

If anyone has any guesses, why is it like that and how to fix it, please help!

Link to comment
Share on other sites

Works fine for me in an auto-load module in ProcessWire 3.0.42 using the code below. All fields are saved correctly, both page and non-page fields.

public function init() {
  $this->pages->addHookBefore("FieldtypeMulti::savePageField", $this, "hookBefore");
}

public function hookBefore(HookEvent $event) {
  $page = $event->arguments[0];
  $this->newPage($page);// calls method to create new page
  $this->message("we hooked into page with id: {$page->id}");
}	

public function newPage($parent) {
  $p = new Page();
  $p->template = 'basic-page';
  $p->parent = $parent;
  $p->title = 'Test Child Page';
  $p->save();
}

 

  • Like 3
Link to comment
Share on other sites

  • 11 months later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...