Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Problem with Page->duplicateWithChildren()

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

Page: 1
Go to End
Author Topic: 1011 Views
  • Motoma
    Avatar
    Community Member
    7 Posts

    Problem with Page->duplicateWithChildren() Link to this post

    I have a class 'CourseTemplatePage' which extends the Page class. I want to create a clone of this page when the CloneAsTemplate action is called.

    function CloneAsTemplate($arguments)
    {
       $data = $arguments->allParams();
       if($ct = DataObject::get_by_id('CourseTemplatePage', $data['ID']))
       {
          $newct = $ct->duplicateWithChildren();
          $newct->Title .= ' (Clone)';
          $newct->writeToStage('Stage');
          $newct->publish('Stage', 'Live');
       }
       Director::redirectBack();
    }

    The this code gets to the publish command, an Warning occurs, stating that the record didn't exist in Stage.
    Upon inspecting the database, everything seems to be correct, with proper entries in the CourseTemplatePage, CourseTemplatePage_Live, CourseTemplatePage_version, etc...

    However, when I took a look at the record in SiteTree, I saw the record had a ClassName of 'SiteTree' instead of 'CourseTemplatePage'. Looking at the code for the Versioned class, it appears that the class name must properly match in order for publish to work.

    Is there something wrong in my code that is causing duplicateWithChildren() to incorrectly classify the CourseTemplatePage? Or is there something else that I am glossing over? Any help would be appreciated.

    Thanks,
    M

  • theAlien
    Avatar
    Community Member
    131 Posts

    Re: Problem with Page->duplicateWithChildren() Link to this post

    just curious:
    have you tried to duplicate with children manually (by rightclicking in the sitetree-pane)?
    if you do so, what happens (especially in SiteTree database)?

  • Motoma
    Avatar
    Community Member
    7 Posts

    Re: Problem with Page->duplicateWithChildren() Link to this post

    Thanks for your response.

    When I clone a page and its children though the CMS SiteTree pane, the SiteTree table has ClassName appropriately set--in this case CourseTemplatePage.

    Am I using the duplicateWithChildren method correctly? Is my publishing code set up properly? Is there something in my class that could alter the way duplicateWithChildren works?

    Thanks again for your help,
    M

  • Motoma
    Avatar
    Community Member
    7 Posts

    Re: Problem with Page->duplicateWithChildren() Link to this post

    Here's what I have:

    Inside my CourseTemplatePage class I copied the duplicateWithChildren method from SiteTree, but with one subtle difference: when the initial object's duplicate call is made, my method specifies that the duplicate should not have the write() method called:

    public function duplicateWithChildren()
    {
       // Changed this line to specify that the write() method should not be called
       // Without this, writeToStage('Stage') doesn't work properly.
       // $clone = $this->duplicate();
       $clone = $this->duplicate(False);
       $children = $this->AllChildren();

       if($children) {
          foreach($children as $child) {
             $childClone = method_exists($child, 'duplicateWithChildren')
                ? $child->duplicateWithChildren()
                : $child->duplicate();
             $childClone->ParentID = $clone->ID;
             $childClone->write();
          }
       }

       return $clone;
    }

    If someone reads this and understands why this fixes the problem I was having, please let me know as this is something I would like to get my head around.

    Cheers,
    M

    1011 Views
Page: 1
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.