BUCKHORN Posted October 27, 2013 Share Posted October 27, 2013 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 More sharing options...
apeisa Posted October 27, 2013 Share Posted October 27, 2013 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 == 1 Link to comment Share on other sites More sharing options...
BUCKHORN Posted October 27, 2013 Author Share Posted October 27, 2013 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. 1 Link to comment Share on other sites More sharing options...
adrian Posted October 28, 2013 Share Posted October 28, 2013 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. 1 Link to comment Share on other sites More sharing options...
BUCKHORN Posted October 28, 2013 Author Share Posted October 28, 2013 Never knew about this. Good tip. Thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now