Version: current

     1 <?php
     2
     3 class SimpleTreeDropdownField extends HTMLDropdownField
     4 {
     5     protected $sourceClass;
     6     protected $labelField;
     7
     8     function __construct($name, $title = "", $sourceClass = "SiteTree", $value = "", $labelField = "Title", $form = null, $emptyString = null)
     9     {
    10         $this->sourceClass = $sourceClass;
    11         $this->labelField = $labelField;
    12         parent::__construct($name, $title, $this->getHierarchy(0), $value, $form, $emptyString);
    13     }
    14
    15     public function setLabelField($field)
    16     {
    17         $this->labelField = $field;
    18     }
    19
    20     private function getHierarchy($parentID, $level = 0)
    21     {
    22         $options = array();
    23         if($children = DataObject::get($this->sourceClass, "ParentID = $parentID")) {
    24             foreach($children as $child) {
    25                 $indent="";
    26                 for($i=0;$i<$level;$i++) $indent .= "&nbsp;&nbsp;";
    27                 $text = $child->__get($this->labelField);
    28                 $options[$child->ID] = empty($text) ? "<em>$indent Untitled</em>" : $indent.$text;
    29                 $options += $this->getHierarchy($child->ID, $level+1);
    30             }
    31         }
    32         return $options;
    33     }
    34 }
    35

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -