Jump to content

Adding child pages creates loop


BUCKHORN
 Share

Recommended Posts

I think i'm missing something obvious here. I want to add child pages each time a group page is created. 

My Group (when this is added, I want to add the two children and set their parent to "My Group")

----Workspace

----Calendar

For some reason it's looping and creating this structure...

My Group

----Workspace

--------Workpsace

------------Workspace ...(infinite) 

Anybody know what's going on here? Is there any content which shows or explains the application flow in PW? I can understand why it's looping on the save method, but I don't understand how PW handles the request from start to finish so I can't figure out how to prevent this.

public function hookAdded(HookEvent $event) {
  // this function only called after a new page added

  $page = $event->arguments(0);
  $templates = wire('templates');

  if($page->template = 'group') {
 
   // todo: move to array and loop
   $workspace = new Page();
   $workspace->template = $templates->get("workspace");
   $workspace->parent = $page;
   $workspace->title = "Workspace";
   $workspace->save();
  
   $calendar = new Page();
   $calendar->template = $templates->get("calendar");
   $calendar->parent = $page;
   $calendar->title = "Calendar";
   $calendar->save();
  
  }
}
Link to comment
Share on other sites

you are probably hooking into page save (you left the actual hook away from code). When you create new page, then it is also saved and hook gets repeated.

Simple way of preventing is adding runtime property to your new page:

$calendar->skip = true;

and check for that property before creating new page.

EDIT: I see you have template check there that should do the same - but it has a bug in it:

if($page->template = 'group')

should be with ==

  • Like 1
Link to comment
Share on other sites

Thanks Apeisa! I knew it was something simple. I've been coding in php for years, and in the last couple of weeks I've been making this mistake repeatedly. I think it's because I've been coding more javascript which uses a single =. At any rate, thanks for the assist. 

  • Like 1
Link to comment
Share on other sites

I can't seem to make myself use this on the regular basis, but reversing things so you have:

 if('group' == $page->template)

This works well, because if you forget the second = you get an error, rather than php just setting the variable.

  • 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...