Jump to:

791 Posts in 369 Topics by 303 members

Widgets

SilverStripe Forums » Widgets » Generating WidgetArea on the fly

Discuss SilverStripe Widgets.

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

Page: 1
Go to End
Author Topic: 983 Views
  • Pigeon
    Avatar
    Community Member
    243 Posts

    Generating WidgetArea on the fly Link to this post

    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
             $widgets->push($this->Widgets());
             
             $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){
                $widgets->push($widge);
             }
             
             
             //unset($moreWidgets);
             
             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

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