diegonella Posted June 5, 2012 Share Posted June 5, 2012 Hi all, There is a possibility within processwire that when creating a page with a specified template in the same generated automatically create two sub-pages? New Page - Child 1 <- automatic - Child 2 <- automatic Link to comment Share on other sites More sharing options...
apeisa Posted June 5, 2012 Share Posted June 5, 2012 I have few modules that does that. Nothing generic, but I'll post you examples when I'm on computer again (tomorrow). Unless someone is faster of course Link to comment Share on other sites More sharing options...
Soma Posted June 5, 2012 Share Posted June 5, 2012 This is possible with a simple module that hooks us in after the page "save", then retrieve the page object so you can check for it's template. You can find an example module in your site/modules/HelloWorld.module and look at the hooks and the example2() function. There you also see other examples of hooks. If you're into PW's API it's quite simple as it's the same as if you would code template code, just a different context and maybe more advanced. I don't think there's also a "onfirst" save event that could be hooked. So what you want to have, is a check for if the two pages are already created under the page and omit the creation of the two children pages. 1 Link to comment Share on other sites More sharing options...
porl Posted June 6, 2012 Share Posted June 6, 2012 Perhaps an "on created" hook would be useful here and for other cases? Link to comment Share on other sites More sharing options...
apeisa Posted June 6, 2012 Share Posted June 6, 2012 This should do it (cleaned from other module, so not sure if this works without fixing, but idea should be pretty clear): <?php class Elections extends WireData implements Module { public static function getModuleInfo() { return array( 'title' => 'Elections', 'version' => 101, 'summary' => 'Simple module to demonstrate how to automatically create subpages.', 'singular' => true, 'autoload' => true, ); } public function init() { // add a hook after the $pages->save $this->pages->addHookAfter('save', $this, 'afterPageSave'); } public function afterPageSave($event) { $page = $event->arguments[0]; // We want to create subpage only when using if ($page->template == 'election' && $page->numChildren == 0) { $p = new Page(); $p->template = $this->templates->get("candidates"); $p->parent = $page; $p->title = "Candidates"; $p->sortfield = wire('fields')->get('v_candidate_number'); $p->save(); $p2 = new Page(); $p2->template = $this->templates->get("votes"); $p2->parent = $page; $p2->title = "Votes"; $p2->save(); $this->message("New election created."); } } } 6 Link to comment Share on other sites More sharing options...
diegonella Posted June 6, 2012 Author Share Posted June 6, 2012 thank you very much!! This should do it (cleaned from other module, so not sure if this works without fixing, but idea should be pretty clear): <?php class Elections extends WireData implements Module { public static function getModuleInfo() { return array( 'title' => 'Elections', 'version' => 101, 'summary' => 'Simple module to demonstrate how to automatically create subpages.', 'singular' => true, 'autoload' => true, ); } public function init() { // add a hook after the $pages->save $this->pages->addHookAfter('save', $this, 'afterPageSave'); } public function afterPageSave($event) { $page = $event->arguments[0]; // We want to create subpage only when using if ($page->template == 'election' && $page->numChildren == 0) { $p = new Page(); $p->template = $this->templates->get("candidates"); $p->parent = $page; $p->title = "Candidates"; $p->sortfield = wire('fields')->get('v_candidate_number'); $p->save(); $p2 = new Page(); $p2->template = $this->templates->get("votes"); $p2->parent = $page; $p2->title = "Votes"; $p2->save(); $this->message("New election created."); } } } 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