Jump to:

5450 Posts in 1672 Topics by 1197 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: 4133 Views
  • theAlien
    Avatar
    Community Member
    131 Posts

    Re: Automatically Creating Child Pages Link to this post

    Ah, thanks Aram. I'm still figuring out when things have to go in the controller and when they have to go in the normal extends Page-place.

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Automatically Creating Child Pages Link to this post

    Hi Alien

    Just think of it like this. Anything that has to do with defining the data or setting the data when the page is created is done in the 'Model' as well as any functions which deal with retrieving or storing data from/to that page in the database.

    The 'Controller' is where all of the logic for that page type goes, so all the functions that need to be run while a user opens and browses that page go into the controller.

    Hope that makes it a little clearer, I remember I found it confusing at the start too

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: Automatically Creating Child Pages Link to this post

    the problem comes again

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

       static $allowed_children = array('ProgramDniaPage, SylwetkiHolder, SprawozdaniaHolder');

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

             $program_dnia = new ProgramDniaPage();
             $program_dnia->ParentID = $this->ID;
             $program_dnia->Title = "Program dnia";
             $program_dnia->write();

             $sylwetki = new SylwetkiHolder();
             $sylwetki->ParentID = $this->ID;
             $sylwetki->Title = "Sylwetki";
             $sylwetki->write();

             $sprawozdania = new SprawozdaniaHolder();
             $sprawozdania->ParentID = $this->ID;
             $sprawozdania->Title = "Sprawozdania";
             $sprawozdania->write();

          }
          return parent::onAfterWrite();
       }
    }

    works
    but when i publish NewDzienPage i get three blank pages which are copies of ProgramDniaPage, SylwetkiPage and SprawozdaniaHolder

    look at screen

    SS 2.3.2

    Attached Files
  • snaip
    Avatar
    Community Member
    180 Posts

    Re: Automatically Creating Child Pages Link to this post

    the solution could be first publish the subpages and NewDzienPage at the end
    but i can't find how to change default behaviour

    i tired

       $sprawozdania->Status = "Published";

    and in SprawozdzaniaHolder class

       static $defaults = array(
          "Status" => 'Published'
       );

    but nothing works

  • Tuckie
    Avatar
    Community Member
    10 Posts

    Re: Automatically Creating Child Pages Link to this post

    Old thread, but has anyone gotten this working reliably?

    I get draft and published duplicate children if I try save and publish, and with the "duplicate this page and children" function, I get draft duplicates.

    my code:

       function onAfterWrite() {
          if(!$this->Children()->Count()){
             $subpage = new JobHolder();
             $subpage->ParentID = $this->ID;
             $subpage->Title = "Jobs";
             $subpage->write();
          }
          return parent::onAfterWrite();
       }

  • henkrid
    Avatar
    Community Member
    2 Posts

    Re: Automatically Creating Child Pages Link to this post

    Same problem with same code as Tuckie.
    The problem occures when you "save & publish" the parent-page while the generated child-page is still unpublished.
    When you publish the child-page before the parent-page, everything works fine.

    This problem is caused by the onAfterWrite-Event. During the "save & publish" process it gets triggered four times.
    At fourth time (triggerd by the "real" publish process) $this->Children()->Count() is 0 because the parent-page has no published child yet.

    Here's a reference from silverstripe googlegroups:
    "1. LeftAndMain calls DataObjects writeWithoutVersion which calls write
    2. LeftAndMain calls write on the $record - the actual save I believe
    3. LeftAndMain calls doPublish on SiteTree which calls its own write
    4. Right after that write the publish method is called, a method on Versioned decorator that calls its owner's write method. "

    I have not worked on a fix yet. Anyone has?
    Is there maybee a way to find out which function has triggerd the onAfterWrite-Event?

    bye henkrid

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Automatically Creating Child Pages Link to this post

    I'm not going through the whole thread (so I'm not 100% this will help) but the $obj->write is only good for a plain dataobject, if you want to save AND publish a Page then do this...

    $object->writeToStage('Stage');
    $object->publish('Stage', 'Live');

  • henkrid
    Avatar
    Community Member
    2 Posts

    Re: Automatically Creating Child Pages Link to this post

    Hey swaiba,
    thanks for your suggestion.
    this works out, but the generated child-page gets directly published on creation.

    But i would be nicer if the child-page gets published while publishing the parent-page.
    I am working on it but it's a little tricky because the parent-page has different children in different versions.

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