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


Go to End
Reply

12 Posts   5851 Views

Avatar
snaip

28 January 2009 at 9:01am (Last edited: 28 January 2009 9:04am), Community Member, 180 Posts

static $db = array(
'File' => 'Text'
      
);

function getCMSFields(){
   
      return new FieldSet(
         new TreeDropdownField('File','Image file name','File')

      );          
};

i have the error: "Error getting data"

this solution doesn't work too
http://www.silverstripe.org/archive/show/118008

Avatar
dio5

28 January 2009 at 9:16am Community Member, 501 Posts

Yes, TreeDropdownFields don't work in widgets (yet).

Perhaps you can use a regular dropdownfield instead?

Avatar
snaip

28 January 2009 at 9:34am Community Member, 180 Posts

:(
DropdownField works good but i have folder tree like this:

+warsaw
- castle_image.jpg
- wisla_river.jpg

+krakow
- castle_image.jpg
- mariacki_church_jpg
- wisla_river.jpg

so i have to create two DropdownField to select full URL to the file
one for Folder
second for Image file

i don't want to list all the image files in DropdownField, if i select Warsaw folder i want to list only this files which are in the Warsaw folder

is there any method to create it ?

Avatar
UncleCheese

28 January 2009 at 10:18am 4085 Posts

The TreeDropdownField doesn't work with much. I created a SimpleTreeDropdownField, which is just a regular dropdown menu organized hierarchically. No javascript. Let me know if you want the code. You could use that to ajax-update your second dropdown menu for the files.

Avatar
snaip

28 January 2009 at 10:28am Community Member, 180 Posts

UncleCheese yes please

Avatar
snaip

28 January 2009 at 10:57am Community Member, 180 Posts

ok i have alternative version, maybe it is not very beautiful but i have something like a hierachy

new DropdownField('File_name','Select image:',DataObject::get("File")->toDropdownMap('Filename','Filename'))

Avatar
UncleCheese

28 January 2009 at 11:21am 4085 Posts

Try this for the folder part:

<?php

class SimpleTreeDropdownField extends DropdownField
{
   function __construct( $name, $title, $className = "Folder", $value = null, $form = null, $emptyString = null)
   {
      $this->className = $className;
      $optionArray = $this->getHierarchy(0);
      parent::__construct( $name, $title, $optionArray, $value, $form, $emptyString );
   }
   
   private function getHierarchy($parentID, $level = 0)
   {
      $options = array();      
      if($children = DataObject::get($this->className, "ParentID = $parentID")) {
         foreach($children as $child) {
            $indent="";
            for($i=0;$i<$level;$i++) $indent .= "&nbsp;&nbsp;";
            $options[$child->ID] = $indent.$child->Title;
            $options += $this->getHierarchy($child->ID, $level+1);
         }
      }
      return $options;
   }
}

?>

Avatar
snaip

28 January 2009 at 9:37pm Community Member, 180 Posts

ok, where i should put this code ?

ImageWidget.php

class ImageWidget extends Widget {

[...]

return new FieldSet(
new DropdownField('File_name','Image file: ',$this->getHierarchy)
)

[...]

}

class SimpleTreeDropdownField extends DropdownField {

}


doesn't work

class ImageWidget extends Widget {

[...]

return new FieldSet(
new DropdownField('File_name','Image file: ',$this->getHierarchy)
)

[...]

}

class SimpleTreeDropdownField extends ImageWidget {

}

i get:

Fatal error: Declaration of SimpleTreeDropdownField::__construct() must be compatible with that of DataObjectInterface::__construct() in /home/wilgocki/domains/wilgocki.net/public_html/silverstripe/widgets_ImageWidget/ImageWidget.php on line 94

my previous code works but i need to wrap words after 50 chars (http://www.silverstripe.org/customising-the-cms/show/253177?showPost=253177)

new DropdownField('File_name','Select image:',DataObject::get("File")->toDropdownMap('Filename',wordwrap('Filename', 50, "\n", 1)))

Go to Top