Jump to content

Adding a page after a new user was added


pout
 Share

Recommended Posts

Hallo,

I tried to hook the adding of a new User to get a new subpage saved as a sort of user directory. But I didn't get it. First I tried Page::saved in ready.php (limitation on User-page ist missing). I want to get the name of the user added before and save a page under /Abos with the user-name as page title.:

wire()->addHookAfter('Page::saved', function($event) {
    $savedUser = $event->object;
    $userpage = wire('pages')->find("parent=Abos, name=".$savedUser->name); 
    if ( count($userpage) == 0 ) {
        $page = new Page();
        $page->template = 'Abos';
        $page->parent = wire('pages')->get('/Abos');
        $page->title = $savedUser->name;
        $page->save();
    }
});

but this leads in never stopping new added pages under /Abos.

Then I tried the same code with:

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

That doesn't work.

Hints appreciated. Thanks.

 

Link to comment
Share on other sites

You are getting into a looping issue because you are hooking into every page saved. I also think you have to hook into Pages which then has the need info in its arguments.

wire()->addHookAfter('Pages::saved', function($event) {
	$page = $event->arguments(0);
	if($page->template == "user"){
          $savedUser = $page;
          $userpage = wire('pages')->find("parent=Abos, name=".$savedUser->name); 
          if ( count($userpage) == 0 ) {
              $page = new Page();
              $page->template = 'Abos';
              $page->parent = wire('pages')->get('/Abos');
              $page->title = $savedUser->name;
              $page->save();
          }
	}
});

 

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