Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Inheriting widgets

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

Page: 1
Go to End
Author Topic: 2767 Views
  • joshy
    Avatar
    Community Member
    57 Posts

    Re: Inheriting widgets Link to this post

    Hiya,

    Not sure this is the 'right' way but it certainly works with a lot less code!

    If the page you are viewing has widgets, it will use them. If it doesn't have any, it will try use the parent's widgets. If the parent has none it will use the widgets of the Homepage (URL segment = 'home').

    This presumes you call $Sidebar in the template and that you have set the $has_one like so:

    $has_one = array('WidgetArea' => 'WidgetArea');

    function Sidebar()
    {
    if ($this->WidgetArea()->Widgets()->Count())
    {
    return $this->WidgetArea();
    }
    else if ($this->parent()->exists() && $this->parent()->WidgetArea()->Widgets()->Count())
    {
    return $this->parent()->WidgetArea();
    }
    else
    {
    $home = DataObject::get_one("Page", "URLSegment = 'home'");
    return $home->WidgetArea();
    }
    }

    I apologise in advance if this has syntax errors - I am not near a SS install to test.

    Cheers,

    Josh

    ps: my indentations are not showing. Grr...

  • joshy
    Avatar
    Community Member
    57 Posts

    Re: Inheriting widgets Link to this post

    Something like this should work (untested, sorry - again, not at near a Silverstripe install):

    function Sidebar()
    {
       // Create a new object
       $widgets = new DataObjectSet();

       // Push this page's widgets into it
       if ($this->WidgetArea()->Widgets()->Count())
       {
          $widgets->push($this->WidgetArea());
       }
       // Push the parent widgets into it too
       if ($this->parent()->exists() && $this->parent()->WidgetArea()->Widgets()->Count())
       {
          $widgets->push($this->parent()->WidgetArea());
       }

       // ** You could potentially get the parent's parent or the homepage's or whatever too...

       // Uncomment the debug line to show the contents of the widget variable (so you know what to reference in the template)
       // Debug::show($widgets);
       return $widgets;
    }

    You'd then call this as a <% control Sidebar %> in your template and loop through the values (as shown in the Debug::show - currently commented out).

    I hope this helps!

    Cheers,

    Josh

  • joshy
    Avatar
    Community Member
    57 Posts

    Re: Inheriting widgets Link to this post

    It's alright - I've spent the last few weeks messing around with similar things and it did my head in - glad I can share my knowledge and make your life a bit easier .

    Cheers,

    Josh

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