Jump to:

794 Posts in 409 Topics by 304 members

Widgets

SilverStripe Forums » Widgets » Inherit Widgets

Discuss SilverStripe Widgets.

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

Page: 1 2
Go to End
Author Topic: 3461 Views
  • tzeller
    Avatar
    Community Member
    26 Posts

    Inherit Widgets Link to this post

    i am wondering if there is any possiblity that i can automatically inherit a widget from the parent to all children?

  • Nivanka
    Avatar
    Community Member
    395 Posts

    Re: Inherit Widgets Link to this post

    what do you mean? do you need to add a widget to all the pages or what?

  • tzeller
    Avatar
    Community Member
    26 Posts

    Re: Inherit Widgets Link to this post

    i have added the widgets to all my pages (page.php and other modules such as ecommerce, etc.). i am on silverstripe 2.3 rc2.

    And now instead of adding the widgets individually page by page i would like to define by default that the parent page will inherit the widget to its child pages if they don't have any (inherit as sitetree).

    thanks

  • Nivanka
    Avatar
    Community Member
    395 Posts

    Re: Inherit Widgets Link to this post

    You can edit the Page.php, and add some methods then it will install the widgets on your pages.

    Check how the blog module has done,

    let me know I will post something if you need.

  • tzeller
    Avatar
    Community Member
    26 Posts

    Re: Inherit Widgets Link to this post

    thanks nivanka for you quick response.

    do i get this right. you mean above function from the "BlogHolder.php", right?
    -----
    function requireDefaultRecords() {
          parent::requireDefaultRecords();
          
          if(!DataObject::get_one('BlogHolder')) {
             $blogholder = new BlogHolder();
             $blogholder->Title = "Blog";
             $blogholder->URLSegment = "blog";
             $blogholder->Status = "Published";
             
             $widgetarea = new WidgetArea();
             $widgetarea->write();
             
             $blogholder->SideBarID = $widgetarea->ID;
             $blogholder->write();
             $blogholder->publish("Stage", "Live");
    ----

    change it in ?

    function requireDefaultRecords() {
          parent::requireDefaultRecords();
          
          if(!DataObject::get_one('Page')) {
             $blogholder = new BlogHolder();
             $blogholder->Title = "Blog";
             $blogholder->URLSegment = "blog";
             $blogholder->Status = "Published";

             
             $widgetarea = new WidgetArea();
             $widgetarea->write();
             
             $page/color]->SideBarID = $widgetarea->ID;
             $page/color]->write();
             $page/color]->publish("Stage", "Live");

    i have successfully added widget to all my pages and can place individually the widgets per page. now i want that my parent page (ie "about us") will inherit its widgets to its children pages (ie "company profile" in the site tree of about us).
    thanks

  • Nivanka
    Avatar
    Community Member
    395 Posts

    Re: Inherit Widgets Link to this post

    Yes thats what I meant.

  • tzeller
    Avatar
    Community Member
    26 Posts

    Re: Inherit Widgets Link to this post

    i don't know what i am doing wrong.

    this is the code i added to my page.php:

       function requireDefaultRecords() {
          parent::requireDefaultRecords();
          
          if(!DataObject::get_one('Page')) {
             $widgetarea = new WidgetArea();
             $widgetarea->write();
             
             $page->SideBarID = $widgetarea->ID;
             $page->ParentID = $page->ID;
             $page->write();
             $page->publish("Stage", "Live");
             
             $widgetarea->write();
          }
       
       }

    there is no change - i still can add my widgets page by page... but they are not inheriting from parent-pages to children-pages.

    hopefully somebody can help my.

    thanks

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Inherit Widgets Link to this post

    You could also make a method for displaying on the templates only, in which you loop through the parents checking for widgets. There would be no real widget saved in the db for that page, but it would be enough for displaying them.

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