Jump to:

796 Posts in 409 Topics by 304 members

Widgets

SilverStripe Forums » Widgets » Is there a better way to add the same widgets to all pages?

Discuss SilverStripe Widgets.

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

Page: 1 2
Go to End
Author Topic: 2360 Views
  • MikeOne
    Avatar
    Community Member
    40 Posts

    Is there a better way to add the same widgets to all pages? Link to this post

    Hi,

    I have been struggeling quite a lot (I'm a newbie) to add a sidebar to all pages of the site that contains the same widgets.
    The requirements are:

    1) Have one place in the CMS to add widgets
    2) Show those widgets on all pages of pagetype 'Page' or anything that inherits from it.

    Now - I have got it working, but I was wondering if there is a better way of doing it. This is what I did:

    Created a 'SidebarPage' page type that allows the editors to add the widgets:

    --------------------------------------------

    class SidebarPage extends Page{

       static $has_one = array(
        "Sidebar" => "WidgetArea",
       );
       
       static $defaults = array(
          'ShowInMenus' => 0,
          'ShowInSearch' => 0
       );
       
       function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab("Root.Content.Widgets", new WidgetAreaEditor("Sidebar"));
        return $fields;
       }
       
    }

    class SidebarPage_Controller extends Page_Controller{

    }
    --------------------------------------------

    Added a SidebarPage.ss:

    --------------------------------------------

    $Sidebar

    --------------------------------------------

    And then in mysite/code/page.php - page_controller I added:

    --------------------------------------------

       function getSidebar(){
          $sidebarPage = DataObject::get_one('SidebarPage');   
          return ($sidebarPage) ? $sidebarPage->renderWith("SidebarPage") : false;
       }

    --------------------------------------------

    Now in Page.ss (or any other template), I can render the sidebarwidgets by simply adding $getSideBar in the content.

    Especially the 'renderWith' part took ages for me to find out. Is there a simpler or better way to do this? Or am I spot on?

    Thanks,
    Mike.

  • MikeOne
    Avatar
    Community Member
    40 Posts

    Re: Is there a better way to add the same widgets to all pages? Link to this post

    Nobody has any feedback on this? I really like to know if this is implemented 'correctly'. I know it's working but what I don't know if there is an easier way - or if I didn't break MVC principles etc...

    Thanks!

    Mike.

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: Is there a better way to add the same widgets to all pages? Link to this post

    Hi MikeOne,

    I would say it's a bit of an odd way to do it, certainly not how i would.

    This is how i did my WidgetArea so that pages that didn't have widgets defined:

    function myWidgets() {
          if(!$this->Widgets()->Widgets()->Count()) { // if no widgets are assigned for the page, then we get the parent's widget
             if ($this->Parent()->exists() && $this->Parent()->Widgets()->Widgets()->Count()) //if the parent has widgets, we get them
             {
                return $this->Parent()->Widgets();
             }
             else { // if the parent doesnt have widgets, just pick some random ones
                return DataObject::get_one('WidgetArea', /* Make sure that we pick a widget area that actually has widgets */'`WidgetArea`.ID IN (SELECT DISTINCT `ParentID` FROM `Widget`)','RAND()');
             }
          }
          else { //there are widgets for this page
             return $this->Widgets();
          }
       }

    So instead of calling $Sidebar in my template, i call $myWidgets.

    I'm not sure why you are using render with, etc. Surely all you need to to is grab the relevant WidgetArea and then output it in the place specified by the template...

  • MikeOne
    Avatar
    Community Member
    40 Posts

    Re: Is there a better way to add the same widgets to all pages? Link to this post

    Ah cool - thanks for the reply.

    The way you do it seems cleaner, however - where do I add my widget tab in the CMS in your case? The site has quite a flat structure so i need to be able to show widgets NOT from a parent - but from a sibbling. Does that make sense?

    So suppose I have 3 pages:

    - home
    - contact us
    - about us

    I need one place where I can add widgets - and they will have to show on all of these 3 pages. Will your solution cater for that?

    Thanks again for your help.

    Mike.

  • MikeOne
    Avatar
    Community Member
    40 Posts

    Re: Is there a better way to add the same widgets to all pages? Link to this post

    I wonder though if a combination of you code and my code is possible.. the reason why I had to use renderWith (I guess??) is because without it - it would just return a reference to the sidebar - but without a view attached to it.

    maybe i could do the following:

    instead of:

    function getSidebar(){
    $sidebarPage = DataObject::get_one('SidebarPage');
    return ($sidebarPage) ? $sidebarPage->renderWith("SidebarPage") : false;
    }

    I could do:

    function getSidebar(){
    $sidebarPage = DataObject::get_one('SidebarPage');
    return ($sidebarPage) ? $sidebarPage->Widgets() : false;
    }

    Does that make sense? i will try this this evening and see if it works..

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: Is there a better way to add the same widgets to all pages? Link to this post

    From your earlier reply i see that you are trying to define the widgets on one page and then use those widgets everywhere, where as I have widgets for all the pages and then if the widgets arent defined for a particular page, i go and get them.

    Yer, give combining them a go.

    But for your return condition you need:

    return ($sidebarPage->exists()) ? $sidebarPage->Widgets() : false;

  • MikeOne
    Avatar
    Community Member
    40 Posts

    Re: Is there a better way to add the same widgets to all pages? Link to this post

    Thanks for the advice - I will give this a go!

  • yurigoul
    Avatar
    Community Member
    202 Posts

    Re: Is there a better way to add the same widgets to all pages? Link to this post

    From the looks of it, this looks like a great way to get one place to add and edit widgets for all the pages in your site.

    The only thing I am wondering about is if it would be possible to get rid of the other tabs (Main - Metadata)

    Without those tabs it would be much clearer what the function of the page is.

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