Version: current

     1 <?php
     2
     3 /**
     4  * A {@link DropdownField} that allows HTML in its option tags
     5  * Overloads {@link DropdownField::Field()} to omit Convert::raw2xml
     6  * @package DataObjectManager
     7  */
     8
     9 class HTMLDropdownField extends DropdownField
    10 {
    11     /**
    12      * Returns a <select> tag containing all the appropriate <option> tags.
    13      * Makes use of {@link FormField->createTag()} to generate the <select>
    14      * tag and option elements inside is as the content of the <select>.
    15      *
    16      * @return string HTML tag for this dropdown field
    17      */
    18     function Field() {
    19         $options = '';
    20
    21         $source = $this->getSource();
    22         if($source) {
    23             // For SQLMap sources, the empty string needs to be added specially
    24             if(is_object($source) && $this->emptyString) {
    25                 $options .= $this->createTag('option', array('value' => ''), $this->emptyString);
    26             }
    27
    28             foreach($source as $value => $title) {
    29
    30                 // Blank value of field and source (e.g. "" => "(Any)")
    31                 if($value === '' && ($this->value === '' || $this->value === null)) {
    32                     $selected = 'selected';
    33                 } else {
    34                     // Normal value from the source
    35                     if($value) {
    36                         $selected = ($value == $this->value) ? 'selected' : null;
    37                     } else {
    38                         // Do a type check comparison, we might have an array key of 0
    39                         $selected = ($value === $this->value) ? 'selected' : null;
    40                     }
    41
    42                     $this->isSelected = ($selected) ? true : false;
    43                 }
    44
    45                 $options .= $this->createTag(
    46                     'option',
    47                     array(
    48                         'selected' => $selected,
    49                         'value' => $value
    50                     ),
    51                     $title
    52                 );
    53             }
    54         }
    55
    56         $attributes = array(
    57             'class' => ($this->extraClass() ? $this->extraClass() : ''),
    58             'id' => $this->id(),
    59             'name' => $this->name,
    60             'tabindex' => $this->getTabIndex()
    61         );
    62
    63         if($this->disabled) $attributes['disabled'] = 'disabled';
    64
    65         return $this->createTag('select', $attributes, $options);
    66     }
    67
    68 }

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