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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Automatically Creating Child Pages


Go to End


27 Posts   8787 Views

Avatar
Ben Gribaudo

Community Member, 181 Posts

4 March 2009 at 10:41am

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

Community Member, 181 Posts

14 March 2009 at 7:19am

Edited: 14/03/2009 7:19am

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

Community Member, 181 Posts

18 March 2009 at 1:18am

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

Community Member, 181 Posts

18 March 2009 at 1:58am

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

Community Member, 181 Posts

18 March 2009 at 2:07am

Edited: 18/03/2009 2:08am

nothing :(

sometimes i hate SS :[

Avatar
Carbon Crayon

Community Member, 598 Posts

18 March 2009 at 3:15am

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

Avatar
snaip

Community Member, 181 Posts

18 March 2009 at 7:05am

Edited: 18/03/2009 7:12am

yeeeee it works in 2.3 :)

thx xD

Avatar
snaip

Community Member, 181 Posts

18 March 2009 at 9:49am

Edited: 20/03/2009 10:59pm

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