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, Ed, biapar, Willr, Ingo, swaiba

Generating WidgetArea on the fly

Go to End



Community Member, 253 Posts

29 September 2009 at 10:44am

Edited: 29/09/2009 10:50am

Hi all,

I'm wondering if it is possible to generate a WidgetArea on the fly so that I can add widgets to Pages automatically.

At the moment I want to have at least 3 widgets on a page and if there is less than 3 widgets on a page then I want to 'push' some more widgets into the area.

So far I have tried:

function myWidgets() {
$num = $this->Widgets()->Widgets()->Count();
if ($num < $this->minWidgets){ //if the number of widgets is less than the minimum number defined
			$widgets = new DataObjectSet();
			//pushing any existing widgets into it
			$moreWidgets = WidgetArea::get('Widget','Widget.ID NOT IN (SELECT `ID` FROM `Widget` WHERE `ParentID` = '.$this->WidgetsID.')',null,null,$this->minWidgets - $num);
			foreach ($moreWidgets as $widge){
			return $widgets;
else {
			return $this->Widgets();

But this doesn't really work. The ultimate goal is to select some widgets that aren't already selected for that page at random.

Is this even possible?

When the else clause returns, the widgets work fine :)

Any help is much appreciated