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

Maximum Number of Widgets in WidgetArea


Reply


4 Posts   911 Views

Avatar
bartvanirsel

Community Member, 94 Posts

12 March 2011 at 3:21am

I'm trying to limit the number of Widgets which get displayed in a WidgetArea.
This always needs to be 6 widgets.

I can edit WidgetArea in core to have a maximum of 6 in the getComponents method but i prefer to decorate WidgetArea.

I did the following:

MyWidgetAreaDecorator.php

class MyWidgetAreaDecorator extends DataObjectDecorator {

   function extraStatics() {
      return array("db" => array('Dummy' => 'Varchar(8)'));
   }

   function WidgetControllers() {
      $controllers = new DataObjectSet();

      foreach($this->ItemsToRender() as $widget) {
         // find controller
         $controllerClass = '';
         foreach(array_reverse(ClassInfo::ancestry($widget->class)) as $widgetClass) {
            $controllerClass = "{$widgetClass}_Controller";
            if(class_exists($controllerClass)) break;
         }
         $controller = new $controllerClass($widget);
         $controller->init();
         $controllers->push($controller);
      }

      return $controllers;
   }

   function ItemsToRender() {
return $this->getComponents('Widgets', "\"Widget\".\"Enabled\" = 1", "", "", 6);
   }

}

in _config.php:

Object::add_extension('WidgetArea', 'MyWidgetAreaDecorator');

Upon dev/build the Dummy varchar gets added, so the decorator is active. Why doest my limit not work? Any clues?

Best regards,

Bart

Avatar
swaiba

Forum Moderator, 1799 Posts

12 March 2011 at 6:08am

Hi,

just because you decorate doens't mean everything function can be overridden (i think)... best to check that your function is being called...

function ItemsToRender() {
   Debug::show('I am here');
   return $this->getComponents('Widgets', "\"Widget\".\"Enabled\" = 1", "", "", 6);
}

if nothing comes through consider inheriting the class, then overriding your method

class MyWidgetArea extends WidgetArea

putting this in your _config.php

Object::useCustomClass('MyWidgetArea','WidgetArea');

Avatar
bartvanirsel

Community Member, 94 Posts

12 March 2011 at 6:22am

Thanks swaiba!

It fixed the problem, have to also redefine MyWidgetArea in the pages containing it and refill the Widgets but is works!

The debug did not show when decorating it. Is there any rule when to use decorators and what they can extend?

Cheers,

Bart

Avatar
swaiba

Forum Moderator, 1799 Posts

12 March 2011 at 6:29am

it is something to do with the core class calling $this->extend('methodname'); in the methodname that can be decorated... do a search for $this->extend in sapphire and you will see all the usual suspects that get decorated.