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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Widgets /

Discuss SilverStripe Widgets.

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

Maximum Number of Widgets in WidgetArea


Go to End


4 Posts   1958 Views

Avatar
bartvanirsel

Community Member, 96 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, 1899 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, 96 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, 1899 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.