Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Widgets

Discuss SilverStripe Widgets.

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

Inherit Widgets


Go to End
Reply

12 Posts   3482 Views

Avatar
tzeller

24 January 2009 at 1:12am Community Member, 26 Posts

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

Avatar
Nivanka

24 January 2009 at 3:28pm Community Member, 395 Posts

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

Avatar
tzeller

25 January 2009 at 1:17am Community Member, 26 Posts

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

Avatar
Nivanka

25 January 2009 at 1:56am Community Member, 395 Posts

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.

Avatar
tzeller

25 January 2009 at 2:24am (Last edited: 25 January 2009 2:31am), Community Member, 26 Posts

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

Avatar
Nivanka

25 January 2009 at 7:10am Community Member, 395 Posts

Yes thats what I meant.

Avatar
tzeller

2 February 2009 at 4:48am Community Member, 26 Posts

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

Avatar
dio5

2 February 2009 at 6:14am Community Member, 501 Posts

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.

Go to Top