gottberg Posted August 2, 2022 Share Posted August 2, 2022 Hi, I have a custom module i made a few years ago from where the users can add pages and PW automatically adds some children for them. The children have some of the values same as the parent. Now I need to update some fields for the children always when the parent is updated. I have never worked with PW hooks before but if iI understand them correctly it should do the trick? Could someone help me and give a few pointers so I can get a grasp on how I should do it? ? Simon Link to comment Share on other sites More sharing options...
elabx Posted August 3, 2022 Share Posted August 3, 2022 This doc page has a very good example on how to setup a hook when the page is saved: https://processwire.com/api/ref/pages/saved/#pwapi-hooking, so something like this placed on site/ready.php should work: $wire->addHookAfter('Pages::saved', function(HookEvent $event) { $page = $event->arguments(0); // parent-page-template is the name of the template that is parent of the pages you want to edit if($page->template != "parent-page-template") return; $page->children->each(function($child) use ($page){ $child->another_field = $page->another_field; $child->save('another_field'); /* another option I like when setting multiple fields $child->setAndsave([ 'title' => $page->title, 'another_field' => $page->another_field ]); */ }); }); Link to comment Share on other sites More sharing options...
gottberg Posted August 3, 2022 Author Share Posted August 3, 2022 Hi, Great, thanks for the help! 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