Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Automatically Creating Child Pages

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

Page: 1 2 3 4
Go to End
Author Topic: 4418 Views
  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Automatically Creating Child Pages Link to this post

    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

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Re: Automatically Creating Child Pages Link to this post

    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();
       }

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: Automatically Creating Child Pages Link to this post

    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();
    }

    }

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Re: Automatically Creating Child Pages Link to this post

    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

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: Automatically Creating Child Pages Link to this post

    nothing

    sometimes i hate SS :[

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Automatically Creating Child Pages Link to this post

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

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: Automatically Creating Child Pages Link to this post

    yeeeee it works in 2.3

    thx xD

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: Automatically Creating Child Pages Link to this post

    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 ? !

    4418 Views
Page: 1 2 3 4
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.