Jump to content

Automatically generating sub page


diegonella
 Share

Recommended Posts

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

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.

  • Like 1
Link to comment
Share on other sites

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.");
 }
}
}
  • Like 6
Link to comment
Share on other sites

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

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...