Jump to:

794 Posts in 408 Topics by 304 members

Widgets

SilverStripe Forums » Widgets » Possible bug in the duplicating page and children functionality

Discuss SilverStripe Widgets.

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

Page: 1
Go to End
Author Topic: 1733 Views
  • teejay
    Avatar
    Community Member
    63 Posts

    Possible bug in the duplicating page and children functionality Link to this post

    Hi there I have just a bug, but I dont know if its expected behavior. There is the following scenario.

    Your page class has a widget area (which is the relation to that forum).

    You have a holder page (inherits page) with lots of child (inherits page) pages. Then u duplicate the holder with all its childs. This means for me I create new pages, and it does create new pages. The problem is it does not create an WidgetAreas in the WidgetArea table --> for the new pages. It just refers to the page from which u have duplicated. So if you now change a widget in one holder page you change the widget in other holder page as well, and this is just not how I would expect it. I would like to change my widgets for every page how i need them and thats not possible if u duplicate in that way. Is there something I can do, maybe a setting to force the system to create new widgetAreas for the new pages .

    Atm I need to do that manualy in the database and that is a bit pain in the ass (ups sorry).

    Is that a bug or expected behavior ??

    Thank you in advance.

  • teejay
    Avatar
    Community Member
    63 Posts

    Re: Possible bug in the duplicating page and children functionality Link to this post

    what a shame no one was able to answer this simple question. I had a deeper look in the cms and sappire code. It turns out that the duplicate functions (one page or with children) ignore any relations. I mean it is principle fine to do so, but in case of a has_one relation with an widget area u run in trouble by just duplication the page table entry and keeping the widget area id. Duplication is atm not an option anymore because of this issue, what a shame.

    I could hack the code and create with the duplication of a page on the fly widgetareas but this would require changes to the core, and I would like to avoid that. Is there a easy way to extend the sitetree methods.

    Thx

    Thomas

  • CoolEP
    Avatar
    Community Member
    5 Posts

    Re: Possible bug in the duplicating page and children functionality Link to this post

    hi,

    got the same issue. you are right there's no new WidgetArea-Item created.

    maybe an overwrite of an parent-method could be the answer. something to do with "onBeforeWrite" or something like that...

    what's the method that duplicates these pages? any guesses?

    cheers

  • Baukez
    Avatar
    Community Member
    4 Posts

    Re: Possible bug in the duplicating page and children functionality Link to this post

    This was really annoying me also. Duplicating pages results in just referenced widgets, not duplicated widgets.

    My solution is to extend the duplicate-function on Page class (page.php):

    /**
    * Duplicate this page, and make a copy of its Widgets (instead of copying the reference to it).
    *
    * @return Page
    */
    public function duplicate() {
       $oPage = parent::duplicate();
       
       if($this->hasField("SidebarID") && ($this->getComponent("Sidebar"))) {
          //duplicate the current widgetArea
          $newWidgetArea = $this->getComponent("Sidebar")->duplicate();
          
          //loop through all widgets linked to this wigetArea & connect them to the new WidgetArea
          foreach($this->getComponent("Sidebar")->Items() as $oAreaWidget) {
             $newWidget = $oAreaWidget->duplicate();
             $newWidget->ParentID = $newWidgetArea->ID;
             $newWidget->write();
          }
          
          //connect the new widgetArea to the duplicate page
          $oPage->SidebarID = $newWidgetArea->ID;
       }
       return $oPage;
    }

    Hopefully this helps someone else aswell...

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