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

Multiple Widget Areas. What problem?


Reply


3 Posts   1461 Views

Avatar
tazzydemon

Community Member, 122 Posts

19 March 2012 at 12:56pm

I have seen many people ask if you can have more than one widget area and many more reply that you can't.

I just tried it and you can.. its as easy as this in page.php...

static $has_one = array(
   "Sidebar1" => "WidgetArea",
"Sidebar2" => "WidgetArea",
"Sidebar3" => "WidgetArea"
);

function getCMSFields() {
   $fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Content.Widgets", new WidgetAreaEditor("Sidebar1"));
$fields->addFieldToTab("Root.Content.Widgets", new WidgetAreaEditor("Sidebar2"));
$fields->addFieldToTab("Root.Content.Widgets", new WidgetAreaEditor("Sidebar3"));

And then just splatter the $Sidebar1 $Sidebar2 $Sidebar3 wherever you want.

I just have to figure out how to make a pageholder page so I can start with one set of widgets for all pages.

Avatar
Juan

Community Member, 18 Posts

8 March 2013 at 4:12pm

Edited: 08/03/2013 4:16pm

Hello Tazzy

what version of silvestripe did you use on this?

i've try it on 2.4 but it doesnt work.

Avatar
snel

Community Member, 12 Posts

4 September 2013 at 6:52am

Edited: 04/09/2013 6:54am

I tried this in 3.1-rc1:

HomePage.php

   private static $has_one = array(
      'MainWidgetArea' => 'WidgetArea',
      'SidebarWidgetArea' => 'WidgetArea'
   );

   public function getCMSFields(){
      $fields = parent::getCMSFields();
      $fields->addFieldToTab('Root.Widgets', new WidgetAreaEditor('MainWidgetArea'));
      $fields->addFieldToTab('Root.Sidebar', new WidgetAreaEditor('SidebarWidgetArea'));
      return $fields;
   }

and a template HomePage.ss which reads like this:

<div class="content-container unit size3of4 lastUnit">
<div>
$MainWidgetArea
</div>
<div class="content">$Content</div>
<div>
$Form
$PageComments
</div>
<aside>
$SidebarWidgetArea
</aside>
</div>

Then, I added/removed widgets (wrote two sample widgets that return a string) in the admin-section and experienced strange behavior:

  • Log in to ss/admin
  • create page "HomePage"
  • define Widget 'weather forecast' for Widet-Area
  • publish
  • display page: weather widget is shown
  • define Widget "facebook messages" for "Sidebar"-Widget Area
  • publish
  • display page: both template blocks are shown, but only the weather-widget shows the output from the widget-Controller
  • admin: delete widget from main widget area
  • publish
  • display page: only the facebook-message widget is shown, but now includes the text, which is returned by the Widget-Controller
  • admin: exchange the facebook messages in the sidebar with the weather widget and put the facbook widget into the main widget-area
  • publish
  • display page:both template blocks are shown but this time only the facebook message widget displays the text returned by the widget-ontroler
  • admin: remove the facebook message widget
  • publish
  • display: whe facebook widget ist gone, the thext for the weather widget is shown

From this experience, I concluded that either I misunderstood the widget/template-mechanism or the widget is broken.