Jump to:

794 Posts in 409 Topics by 304 members

Widgets

SilverStripe Forums » Widgets » Maximum Number of Widgets in WidgetArea

Discuss SilverStripe Widgets.

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

Page: 1
Go to End
Author Topic: 876 Views
  • bartvanirsel
    Avatar
    Community Member
    94 Posts

    Maximum Number of Widgets in WidgetArea Link to this post

    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

  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Re: Maximum Number of Widgets in WidgetArea Link to this post

    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');

  • bartvanirsel
    Avatar
    Community Member
    94 Posts

    Re: Maximum Number of Widgets in WidgetArea Link to this post

    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

  • swaiba
    Avatar
    Forum Moderator
    1788 Posts

    Re: Maximum Number of Widgets in WidgetArea Link to this post

    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.

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