Jump to content

Pages::added hook gets called twice


Recommended Posts

Hello,

I noticed that the Pages::added hook gets called twice. PW 3.0.62.

To test , add this to admin.php

wire()->addHookAfter('Pages::added', function($event) {

	bardump('added'); // needs Tracy Debugger

});

Can anyone confirm this?

It gives me trouble when adding a hook that skips the page add step (for users), following Pete's concept. There will always be created 2 new pages which I need to avoid.

Is it a feature or a bug?

 

Link to comment
Share on other sites

1 hour ago, gebeer said:

I noticed that the Pages::added hook gets called twice.

I can't reproduce this. When I hook after Pages:added and dump the name of the added page this only fires once.

1 hour ago, gebeer said:

It gives me trouble when adding a hook that skips the page add step (for users), following Pete's concept. There will always be created 2 new pages which I need to avoid.

In Pete's code a page is added inside the hook. If you hook after a page is added, then add another page in the hook, you will have two pages. I'm surprised this doesn't cause an endless loop actually.

BTW, you can skip the first step of adding a new page by setting the "Name format for children" option on the template of the parent page.

  • Like 1
Link to comment
Share on other sites

@Robin S thank you again for checking.

I reproduced this on a pretty virgin 3.0.62 install. The only hook I have there is the one from my first post which only dumps 'added', nothing else.

Only thing I had added to the blank PW install was a repeater field.

When I remove the repeater field from the template that I use to add a new page, then Pages::added gets only called once.

With the repeater field in the template it gets called twice.

When a new page is created that has a repeater field, then also a new repeater field page is created.
"Session: Created Repeater Page Parent: /logon/repeaters/for-field-157/for-page-2244/"
Hence the 2 calls to Pages::added.

But this still does not explain, why 2 pages get created when I hook into ProcessPageAdd::execute.

Here's my code:

wire()->addHookBefore('ProcessPageAdd::execute', function($event) {

bardump('memberadd');
	$user = wire('user');
	if($user->hasRole('superuser')) return;

	// This is workaround so that only one page gets created on member add
	if($this->session->get('newMember')) {
		$this->session->redirect("../edit/?id=" . $this->session->get('newMember')['id']);
	}
	// end workaround

	if ($this->input->get->parent_id == 29) { // 29 = users parent page
		$parent = $this->pages->get(1019); // members parent page
		$uid = $this->sanitizer->pageName(uniqid());
	    $newUser = new User();
	    $newUser->parent = $parent; // members page
	    $newUser->template = 'member';
	    $newUser->name = $uid;
	    $newUser->addRole('member');
		$newUser->set('admin_theme', 211);
	    $newUser->save();
	    $newUser->name = "uhti{$newUser->id}";
	    $newUser->save();

		$this->session('newMember', ['id' => $newUser->id, 'creatorId' => $user->id]); // used to confirm new member	    
	    $this->session->redirect("../edit/?id={$newUser->id}");
	}

});

This one gets only called once, but 2 member users are created, if I comment out the workaround.

I have no page creation functionality in the Pages::added hook:

	public function memberAddedHook($event) {

		$user = $this->user;
		$userPage = $event->arguments[0];
		if(!$userPage->getChanges('firstname')) return;
		bardump('addedHook');
	}

 

Link to comment
Share on other sites

The hook doesn't create a page. The hook is called if a page is created. If you add a page with a repeater field in its template the hook is called twice, because 2 pages have been created. If you have 2 repeater fields Pages::added will be triggered 3 times and so on. This is not a bug.

  • Like 4
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...