Jump to:

794 Posts in 408 Topics by 304 members

Widgets

SilverStripe Forums » Widgets » Changing the Available Widgets field in the CMS

Discuss SilverStripe Widgets.

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

Page: 1
Go to End
Author Topic: 672 Views
  • primer
    Avatar
    Community Member
    3 Posts

    Changing the Available Widgets field in the CMS Link to this post

    Hello,

    I'm attempting to alter which widgets are displayed in the Available Widgets section of the CMS. I've set up two fields in the CMS, with a has_one relation for each WidgetAreaEditor.

       public function getCMSFields() {
          $f = parent::getCMSFields();
          $f->addFieldToTab('Root.Content.WidgetsFacebook', new WidgetAreaEditor('Facebook'));
          $f->addFieldToTab('Root.Content.WidgetsTwitter', new WidgetAreaEditor('Twitter'));
          return $f;
       }

    In the CMS however, both widgets are available under the Available Widgets section on both tabs. I know that I'll need to extend or overwrite the AvailableWidgets method, but I'm not quite sure how. I can see that the method currently loops through everything as an array and then spits them out to the CMS, and I'd like to be able to assign a widget to a Widget field tab.

    Any suggestions greatly appreciated, thanks!

  • swaiba
    Avatar
    Forum Moderator
    1785 Posts

    Re: Changing the Available Widgets field in the CMS Link to this post

    Hi primer,

    Welcome!

    The method I use for this is...

    class MyWidgetAreaEditor extends WidgetAreaEditor {

       function AvailableWidgets() {
          $arrAllowedWidgets = array('WidgetOne','WidgetTwo');

          $dosWidgets= new DataObjectSet();
          foreach ($arrAllowedWidgets as $class) {
             $dosWidgets->push(singleton($class));
          }
          return $dosWidgets;
       }
    }

    then use that in your Page.php instead of the regular one...

    $fields->addFieldToTab("Root.Content.Widgets", new MyWidgetAreaEditor("SideBar"));

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