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

TreeDropdownField doesn't work with widget


Reply

12 Posts   5852 Views

Avatar
UncleCheese

29 January 2009 at 3:32am 4085 Posts

Why do you have it extending ImageWidget? It's a subclass of DropdownField. Use the code as I gave it to you, and add the class anywhere in your ImageWidget.php file.

Then you can add new SimpleTreeDropdownField to your fieldset.

Avatar
danqxx

9 February 2010 at 3:11pm Community Member, 14 Posts

UncleCheese, thanks very much for the workaround class. I especially like the way you can just change the name from TreeDropdownField to SimpleTreeDropdownField in the fieldset and it works.

Avatar
Garrett

18 May 2011 at 8:44am Community Member, 245 Posts

I too am using SimpleTreeDropdownField in getCMSFields_forPopup() and it works fine (thanks as always @UC). Question, though, in my DataObjectManager, I'd like to get the Title of the selected page in the SiteTree to display instead of the ID. In my DataObject, I've got:

public static $has_one = array(
"LinkedPage" => "SiteTree"
);
.....
public static $field_names = array(
"Title" => "Title",
"PageName" => "Link to Page",
"Content" => "Content"
);
.....
public function PageName() {
$Page = DataObject::get_by_id("SiteTree", $this->LinkedPageID);
return $Page->Title;
}

But obviously I'm getting an "Unknown column 'PageName' in 'field list'" when I click on the page in the CMS. Any idea how I can accomplish what I want here?

Thanks again,
Garrett

Avatar
nicmart

30 November 2012 at 3:33am (Last edited: 30 November 2012 3:34am), Community Member, 1 Post

I've found where is the problem in using the TreeDropDownField in Widgets (in SS 2.4.7), and I have a simple workaround for that.

The problem is that the method Form::handleField() is not able to find and return the field to the ajax call that is thrown when you click on the dropdown.

Since, in cms/javascript/WidgetAreaEditor.js the name of dropdown fields that are in widgets are overwritten with the string "Widget_TDF_Endpoint", it suffices then to modify the sapphire/forms/Form.php and add this few lines to the handleField method (those between START_MOD and END_MOD):

   function handleField($request) {
      $field = $this->dataFieldByName($request->param('FieldName'));
      
      if($field) {
         return $field;
      } else {
         //START_MOD: TreeDropdownField in Widgets
         $fieldName = $request->param('FieldName');
         if ($fieldName == 'Widget_TDF_Endpoint') {
            return new TreeDropdownField($fieldName, null, 'SiteTree');
         }
         //END_MOD
         // falling back to fieldByName, e.g. for getting tabs
         return $this->Fields()->fieldByName($request->param('FieldName'));
      }
   }

I hope this helps.