Jump to:

5542 Posts in 1739 Topics by 1225 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: 4508 Views
  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Automatically Creating Child Pages Link to this post

    That's because there's no check to see if there are children of the page or not, before creating the children.

    Your code should probably look something like this instead:

    function onAfterWrite() {
       if(!$this->Children()) {
          $cp = new ProgramPage();
          $cp->ParentID = $this->ID;
          $cp->Title = "Test Page";
          $cp->write();
       }
       return parent::onAfterWrite();
    }

  • theAlien
    Avatar
    Community Member
    131 Posts

    Re: Automatically Creating Child Pages Link to this post

    Hmmm... something seems to be wrong...

    I'm using SS2.3.0 and added Persons.php and FormContact.php to mysite/code.
    In Persons.php I added

    class Persons_Controller extends Page_Controller {

    function onAfterWrite() {
    if(!$this->Children()) {
    $cp = new FormContact();
    $cp->ParentID = $this->ID;
    $cp->Title = "Contactform";
    $cp->write();
    }
    return parent::onAfterWrite();
    }

    }

    Next I did run a db/build just to be sure.

    After that I logged in to the CMS and choose Persons in the pagetype-dropdown and clicked Go.
    However, only a persons-page is created, without any contactform-subpage.
    I also tried it without if(!$this->Children()){}, but with no luck either.

    Am I making a mistake or am I expecting the wrong behaviour of onAfterWrite() and should I do something else to accomplish this?

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Automatically Creating Child Pages Link to this post

    Hi Alien

    The onAfterWrite() function has to go in your model class, not your controller

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: Automatically Creating Child Pages Link to this post

    class ProgramHolder extends SiteTree {
    static $db = array(
    );
    static $has_one = array(
    );

    static $allowed_children = array('ProgramPage');

    function onAfterWrite() {
    if(!$this->Children()) {

    $cp = new ProgramPage();
    $cp->ParentID = $this->ID;
    $cp->Title = "New day";
    $cp->write();

    }
    return $this->onAfterWrite();
    }

    }

    dosn't create any page, GO button dosen't work

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Automatically Creating Child Pages Link to this post

    you need to return parent::onAfterWrite(); not $this->onAfterWrite();

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: Automatically Creating Child Pages Link to this post

    now i have the same problem like theAlien, only pageholder is created without subpage

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Automatically Creating Child Pages Link to this post

    Hi Snaip

    For some reason $this->children() is returning true even when there are no children. So I added a count on the end and it seems to work now, so my if statement looks like this:

    if(!$this->Children()->count()){

    }

    hope that helps

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: Automatically Creating Child Pages Link to this post

    it works very good now
    thanks a lot )

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