Webylon 3.1 API Docs
  • Package
  • Class
  • Tree
  • Deprecated
  • Download
Version: current
  • 3.2
  • 3.1

Packages

  • auth
  • Booking
  • cart
    • shipping
    • steppedcheckout
  • Catalog
  • cms
    • assets
    • batchaction
    • batchactions
    • bulkloading
    • comments
    • content
    • core
    • export
    • newsletter
    • publishers
    • reports
    • security
    • tasks
  • Dashboard
  • DataObjectManager
  • event
  • faq
  • forms
    • actions
    • core
    • fields-basic
    • fields-dataless
    • fields-datetime
    • fields-files
    • fields-formatted
    • fields-formattedinput
    • fields-relational
    • fields-structural
    • transformations
    • validators
  • googlesitemaps
  • guestbook
  • installer
  • newsletter
  • None
  • photo
    • gallery
  • PHP
  • polls
  • recaptcha
  • sapphire
    • api
    • bulkloading
    • control
    • core
    • cron
    • dev
    • email
    • fields-formattedinput
    • filesystem
    • formatters
    • forms
    • i18n
    • integration
    • misc
    • model
    • parsers
    • search
    • security
    • tasks
    • testing
    • tools
    • validation
    • view
    • widgets
  • seo
    • open
      • graph
  • sfDateTimePlugin
  • spamprotection
  • stealth
    • captha
  • subsites
  • userform
    • pagetypes
  • userforms
  • webylon
  • widgets

Classes

  • CheckboxField
  • CheckboxField_Disabled
  • CheckboxField_Readonly
  • CheckboxSetField
  • DropdownField
  • GroupedDropdownField
  • ListboxField
  • LookupField
  • NullableField
  • OptionsetField
  • ReadonlyField
  • RoomServiceDropdownField
  • SimpleHTMLEditorField
  • SimpleTinyMCEField
  • SimpleWysiwygField
  • StateDropdownField
  • StateProvinceDropdownField
  • TextareaField
  • TextField
  1 <?php
  2 /**
  3  * Dropdown field, created from a <select> tag.
  4  * @package forms
  5  * @subpackage fields-basic
  6  */
  7 class DropdownField extends FormField {
  8     
  9     /**
 10      * @var boolean $source Associative or numeric array of all dropdown items,
 11      * with array key as the submitted field value, and the array value as a
 12      * natural language description shown in the interface element.
 13      */
 14     protected $source;
 15     
 16     /**
 17      * @var boolean $isSelected Determines if the field was selected
 18      * at the time it was rendered, so if {@link $value} matches on of the array
 19      * values specified in {@link $source}
 20      */
 21     protected $isSelected;
 22     
 23     /**
 24      * @var boolean $disabled
 25      */
 26     protected $disabled;
 27     
 28     /**
 29      * @var boolean $hasEmptyDefault Show the first <option> element as
 30      * empty (not having a value), with an optional label defined through
 31      * {@link $emptyString}. By default, the <select> element will be
 32      * rendered with the first option from {@link $source} selected.
 33      */
 34     protected $hasEmptyDefault = false;
 35     
 36     /**
 37      * @var string $emptyString The title shown for an empty default selection,
 38      * e.g. "Select...".
 39      */
 40     protected $emptyString = '';
 41     
 42     /**
 43      * Creates a new dropdown field.
 44      * @param $name The field name
 45      * @param $title The field title
 46      * @param $source An map of the dropdown items
 47      * @param $value The current value
 48      * @param $form The parent form
 49      * @param $emptyString mixed Add an empty selection on to of the {@link $source}-Array 
 50      *  (can also be boolean, which results in an empty string)
 51      *  Argument is deprecated in 2.3, please use {@link setHasEmptyDefault()} and {@link setEmptyString()} instead.
 52      */
 53     function __construct($name, $title = null, $source = array(), $value = "", $form = null, $emptyString = null) {
 54         $this->source = $source;
 55         
 56         if($emptyString) $this->setHasEmptyDefault(true);
 57         if(is_string($emptyString)) $this->setEmptyString($emptyString);
 58     
 59         parent::__construct($name, ($title===null) ? $name : $title, $value, $form);
 60     }
 61     
 62     /**
 63      * Returns a <select> tag containing all the appropriate <option> tags.
 64      * Makes use of {@link FormField->createTag()} to generate the <select>
 65      * tag and option elements inside is as the content of the <select>.
 66      * 
 67      * @return string HTML tag for this dropdown field
 68      */
 69     function Field() {
 70         $options = '';
 71 
 72         $source = $this->getSource();
 73         if($source) {
 74             // For SQLMap sources, the empty string needs to be added specially
 75             if(is_object($source) && $this->emptyString) {
 76                 $options .= $this->createTag('option', array('value' => ''), $this->emptyString, false);
 77             }
 78             
 79             foreach($source as $value => $title) {
 80                 
 81                 // Blank value of field and source (e.g. "" => "(Any)")
 82                 if($value === '' && ($this->value === '' || $this->value === null)) {
 83                     $selected = 'selected';
 84                 } else {
 85                     // Normal value from the source
 86                     if($value) {
 87                         $selected = ($value == $this->value) ? 'selected' : null;
 88                     } else {
 89                         // Do a type check comparison, we might have an array key of 0
 90                         $selected = ($value === $this->value) ? 'selected' : null;
 91                     }
 92                     
 93                     $this->isSelected = ($selected) ? true : false;
 94                 }
 95                 
 96                 $options .= $this->createTag(
 97                     'option',
 98                     array(
 99                         'selected' => $selected,
100                         'value' => $value
101                     ),
102                     Convert::raw2xml($title), 
103                     false
104                 );
105             }
106         }
107         
108         $attributes = array(
109             'class' => ($this->extraClass() ? $this->extraClass() : ''),
110             'id' => $this->id(),
111             'name' => $this->name,
112             'tabindex' => $this->getTabIndex()
113         );
114         
115         if($this->disabled) $attributes['disabled'] = 'disabled';
116 
117         return $this->createTag('select', $attributes, $options);
118     }
119     
120     /**
121      * @return boolean
122      */
123     function isSelected(){
124         return $this->isSelected;
125     }
126   
127     /**
128      * Gets the source array including any empty default values.
129      * 
130      * @return array
131      */
132     function getSource() {
133         if(is_array($this->source) && $this->getHasEmptyDefault()) {
134             return array(""=>$this->emptyString) + (array)$this->source;
135         } else {
136             return $this->source;
137         }
138     }
139   
140     /**
141      * @param array $source
142      */
143     function setSource($source) {
144         $this->source = $source;
145     }
146     
147     /**
148      * @param boolean $bool
149      */
150     function setHasEmptyDefault($bool) {
151         $this->hasEmptyDefault = $bool;
152     }
153     
154     /**
155      * @return boolean
156      */
157     function getHasEmptyDefault() {
158         return $this->hasEmptyDefault;
159     }
160     
161     /**
162      * Set the default selection label, e.g. "select...".
163      * Defaults to an empty string. Automatically sets
164      * {@link $hasEmptyDefault} to true.
165      * 
166      * @param string $str
167      */
168     function setEmptyString($str) {
169         $this->setHasEmptyDefault(true);
170         $this->emptyString = $str;
171     }
172     
173     /**
174      * @return string
175      */
176     function getEmptyString() {
177         return $this->emptyString;
178     }
179 
180     function performReadonlyTransformation() {
181         $field = new LookupField($this->name, $this->title, $this->source);
182         $field->setValue($this->value);
183         $field->setForm($this->form);
184         $field->setReadonly(true);
185         return $field;
186     }
187     
188     function extraClass(){
189         $ret = parent::extraClass();
190         if($this->extraClass) $ret .= " $this->extraClass";
191         return $ret;
192     }
193 }
194 ?>
[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. -
Webylon 3.1 API Docs API documentation generated by ApiGen 2.8.0