Jump to content

Add hook to custom module form changes to update childpages


gottberg
 Share

Recommended Posts

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

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

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...