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

Using TreeDropdownField() in widget admin editor.


Reply

2 Posts   944 Views

Avatar
DubbeleJ

13 September 2012 at 5:36am (Last edited: 13 September 2012 5:47am), Community Member, 9 Posts

Hello,

I have created a widget, in which I want to bind a PageID/SiteTreeID to the widget to load data. In order to select the page ID my first solution was to use the TreeDropdownField(). I have the following code to generate my admin for the widget:

   public function getCMSFields() {
      return new FieldList(
         new TextField("WidgetTitle", "WidgetTitel"),
         new TreeDropdownField("PageID", "Kies pagina voor submenu", "SiteTree")
      );
   }

However when I use this, I get the following error:

Fatal error: Call to a member function FormAction() on a non-object in C:\xampp\htdocs\4happyfeet.nl\framework\forms\FormField.php on line 139

Do I need some additional things in my Widget in order to make this work, like dependencies in Javascript and such?

Any ideas regarding this would be appreciated.

Regards,
Jan Jaap

Avatar
Matty Balaam

31 October 2012 at 7:08am (Last edited: 2 November 2012 1:50am), Community Member, 69 Posts

Possibly too late, but there are several fields missing in the SS3 Widgets module, and it seems TreeDropDownField is one.

To get around this I used a regular DropdownField, you only get a flat list with no nesting of sub menus however:

new DropdownField("PageLinkID", "Page to link to", SiteTree::get()->map())

Unfortunately, I then had another problem, I couldn't get the link into my widget template. I would usually use <a href="$PageLinkID.Link" > but that doesn't seem to work in a widget

After much hacking around I managed to create a function in a WidgetController class

class PageLinkWidget_Controller extends Widget_Controller {
   
      public function SiteTreeLink($pageID){
         $page = SiteTree::get()->byID($pageID)->Link();
         if ($page) return $page;
      }      
}

Then inside the template I used: <a href ="$SiteTreeLink($PageLinkID)">

If there's a simpler way I'm all ears, but it works.