Jump to content

"Add hook after save" and create child page, but do nothing after further saves


louisstephens
 Share

Recommended Posts

So I have been diving into hooks lately, and I am enjoying them thus far. However, I guess I am a bit stumped on how to achieve what I want too. I am trying to set up a hook that would create a new child page when the parent page is saved. However, when you save the parent page a second time, I just need to update the child page without creating multiple child pages. What would be the best way to go about this?

So after rereading my post, I believe it is a bit vague so I will try to explain more. 

The Goal:

  1. Create a page with a template "one".
  2. Once the page is created/saved => create a new child page with the template of "two"
  3. If the parent is saved anytime after, do nothing to the child page (limit the parent page to one child page)

The parent page is really just being used to output content, whereas the child page is being used to pull out the some fields from the parent to be used elsewhere. I might have made this too complicated in my head.

Edited by louisstephens
Trying to provide more clarification.
Link to comment
Share on other sites

If I understand correctly, something  like this should work, let us know if you need more detail: 

wire()->addHookekAfter("Pages:saved", function($e){
	$page = $e->arguments(0);
	if($page->template == "one"){
		$pageTwo = $page->child("template=two");
		if(!$pageTwo->id){
			//create child page code and save!
		}
	}
});

 

  • Like 3
Link to comment
Share on other sites

12 hours ago, louisstephens said:

So I have been diving into hooks lately, and I am enjoying them thus far.

Better late than never ? ? 

You can use Pages::added for this: https://processwire.com/api/ref/pages/added/

And using conditional hooks (https://processwire.com/blog/posts/new-ajax-driven-inputs-conditional-hooks-template-family-settings-and-more/#new-conditional-hooks) it gets even easier and cleaner:

$wire->addHookAfter('Pages::added', function(HookEvent $event) {
  $page = $event->arguments(0);
  bd($page);
});

I always start with the basic hook and test it. In this case this was quite interesting, because it fired twice! The reason is that this page contains a repeater matrix that creates a page for the first (possible) item:

DUTIhxN.png

Then you can modify the hook to be more specific:

$wire->addHookAfter('Pages::added(template=basic-page)', function(HookEvent $event) {
  $page = $event->arguments(0);
  bd($page);
});

Voila, it fires on the right page only:

LQSP92B.png

And it will not fire on any further publish/save/etc events ? 

  • Like 5
Link to comment
Share on other sites

Thanks @bernhard! That was exactly what I was looking for. Now I'll continue down the path of learning hooks, and hopefully stop making mistakes like:

$p = new Page();
$u->template = "scripts";
$u->parent = wire('pages')->get($page->id);
$u->title = "Test";
$u->name = "test";
$u->save();

Which obviously just blows up in your face.?

  • Like 1
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

×
×
  • Create New...