Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS

Automatically Creating Child Pages


Go to End
Reply

27 Posts   4560 Views

Avatar
Ben Gribaudo

4 March 2009 at 10:41am Community Member, 181 Posts

Hello,

Can a page type be configured to automatically create a child page when it is created?

Example: user creates a SpecialPage; a ChildSpecialPage automatically appears underneath the new SpecialPage.

Thank you,
Ben

Avatar
Ben Gribaudo

14 March 2009 at 7:19am (Last edited: 14 March 2009 7:19am), Community Member, 181 Posts

This is possible! http://silverstripe.org/customising-the-cms/show/256077#post256077 clued me in.

In the page under which the child pages should automatically be created:

   public function onAfterWrite() {
      $cp = new ChildPage();
      $cp->ParentID = $this->ID;
      $cp->Title = "Test Page";
      ...
      $cp->write();
      return parent::onAfterWrite();
   }

Avatar
snaip

18 March 2009 at 1:18am Community Member, 180 Posts

hmmm could you show complete code ?

in ProgramHolder i paste yours but it doesn't work

class ProgramHolder extends SiteTree {
static $db = array(
);
static $has_one = array(
);
   
static $allowed_children = array('ProgramPage');
}

class ProgramHolder_Controller extends ContentController {

public function onAfterWrite() {
$cp = new ProgramPage();
$cp->ParentID = $this->ID;
$cp->Title = "Test Page";

$cp->write();
return parent::onAfterWrite();
}

}

Avatar
Ben Gribaudo

18 March 2009 at 1:58am Community Member, 181 Posts

Hi Snaip,

Your code looks similar to mine, except for the location of onAfterWrite(). It needs to be in the ProgramHolder class, not the controller class. It's after ProgramHolder is written that the children should be created.

Ben

Avatar
snaip

18 March 2009 at 2:07am (Last edited: 18 March 2009 2:08am), Community Member, 180 Posts

nothing :(

sometimes i hate SS :[

Avatar
Carbon Crayon

18 March 2009 at 3:15am Community Member, 598 Posts

you are using 2.3 right? There was no onAfterWrite() in previous stable versions.

Avatar
snaip

18 March 2009 at 7:05am (Last edited: 18 March 2009 7:12am), Community Member, 180 Posts

yeeeee it works in 2.3 :)

thx xD

Avatar
snaip

18 March 2009 at 9:49am (Last edited: 20 March 2009 10:59pm), Community Member, 180 Posts

it works but ... it's creating me 4 copies of the same page ? !!
when i create ProgramHolder it adds me one ProgramPage, ok

next log out, log in, and im geting another 3 copies of the new ProgramPage !!

why ? !

Go to Top