1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\ArrayList; |
6
|
|
|
use SilverStripe\View\ArrayData; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Dropdown field, created from a <select> tag. |
10
|
|
|
* |
11
|
|
|
* <b>Setting a $has_one relation</b> |
12
|
|
|
* |
13
|
|
|
* Using here an example of an art gallery, with Exhibition pages, |
14
|
|
|
* each of which has a Gallery they belong to. The Gallery class is also user-defined. |
15
|
|
|
* <code> |
16
|
|
|
* static $has_one = array( |
17
|
|
|
* 'Gallery' => 'Gallery', |
18
|
|
|
* ); |
19
|
|
|
* |
20
|
|
|
* public function getCMSFields() { |
21
|
|
|
* $fields = parent::getCMSFields(); |
22
|
|
|
* $field = DropdownField::create('GalleryID', 'Gallery', Gallery::get()->map('ID', 'Title')) |
23
|
|
|
* ->setEmptyString('(Select one)'); |
24
|
|
|
* $fields->addFieldToTab('Root.Content', $field, 'Content'); |
25
|
|
|
* </code> |
26
|
|
|
* |
27
|
|
|
* As you see, you need to put "GalleryID", rather than "Gallery" here. |
28
|
|
|
* |
29
|
|
|
* <b>Populate with Array</b> |
30
|
|
|
* |
31
|
|
|
* Example model defintion: |
32
|
|
|
* <code> |
33
|
|
|
* class MyObject extends DataObject { |
34
|
|
|
* static $db = array( |
35
|
|
|
* 'Country' => "Varchar(100)" |
36
|
|
|
* ); |
37
|
|
|
* } |
38
|
|
|
* </code> |
39
|
|
|
* |
40
|
|
|
* Example instantiation: |
41
|
|
|
* <code> |
42
|
|
|
* DropdownField::create( |
43
|
|
|
* 'Country', |
44
|
|
|
* 'Country', |
45
|
|
|
* array( |
46
|
|
|
* 'NZ' => 'New Zealand', |
47
|
|
|
* 'US' => 'United States', |
48
|
|
|
* 'GEM'=> 'Germany' |
49
|
|
|
* ) |
50
|
|
|
* ); |
51
|
|
|
* </code> |
52
|
|
|
* |
53
|
|
|
* <b>Populate with Enum-Values</b> |
54
|
|
|
* |
55
|
|
|
* You can automatically create a map of possible values from an {@link Enum} database column. |
56
|
|
|
* |
57
|
|
|
* Example model definition: |
58
|
|
|
* <code> |
59
|
|
|
* class MyObject extends DataObject { |
60
|
|
|
* static $db = array( |
61
|
|
|
* 'Country' => "Enum('New Zealand,United States,Germany','New Zealand')" |
62
|
|
|
* ); |
63
|
|
|
* } |
64
|
|
|
* </code> |
65
|
|
|
* |
66
|
|
|
* Field construction: |
67
|
|
|
* <code> |
68
|
|
|
* DropdownField::create( |
69
|
|
|
* 'Country', |
70
|
|
|
* 'Country', |
71
|
|
|
* singleton('MyObject')->dbObject('Country')->enumValues() |
72
|
|
|
* ); |
73
|
|
|
* </code> |
74
|
|
|
* |
75
|
|
|
* <b>Disabling individual items</b> |
76
|
|
|
* |
77
|
|
|
* Individual items can be disabled by feeding their array keys to setDisabledItems. |
78
|
|
|
* |
79
|
|
|
* <code> |
80
|
|
|
* $DrDownField->setDisabledItems( array( 'US', 'GEM' ) ); |
81
|
|
|
* </code> |
82
|
|
|
* |
83
|
|
|
* @see CheckboxSetField for multiple selections through checkboxes instead. |
84
|
|
|
* @see ListboxField for a single <select> box (with single or multiple selections). |
85
|
|
|
* @see TreeDropdownField for a rich and customizeable UI that can visualize a tree of selectable elements |
86
|
|
|
*/ |
87
|
|
|
class DropdownField extends SingleSelectField |
88
|
|
|
{ |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Build a field option for template rendering |
92
|
|
|
* |
93
|
|
|
* @param mixed $value Value of the option |
94
|
|
|
* @param string $title Title of the option |
95
|
|
|
* @return ArrayData Field option |
96
|
|
|
*/ |
97
|
|
|
protected function getFieldOption($value, $title) |
98
|
|
|
{ |
99
|
|
|
// Check selection |
100
|
|
|
$selected = $this->isSelectedValue($value, $this->Value()); |
101
|
|
|
|
102
|
|
|
// Check disabled |
103
|
|
|
$disabled = false; |
104
|
|
|
if ($this->isDisabledValue($value) && $title != $this->getEmptyString()) { |
105
|
|
|
$disabled = 'disabled'; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return new ArrayData(array( |
109
|
|
|
'Title' => $title, |
110
|
|
|
'Value' => $value, |
111
|
|
|
'Selected' => $selected, |
112
|
|
|
'Disabled' => $disabled, |
113
|
|
|
)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* A required DropdownField must have a user selected attribute, |
118
|
|
|
* so require an empty default for a required field |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public function getHasEmptyDefault() |
123
|
|
|
{ |
124
|
|
|
return parent::getHasEmptyDefault() || $this->Required(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param array $properties |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function Field($properties = array()) |
132
|
|
|
{ |
133
|
|
|
$options = array(); |
134
|
|
|
|
135
|
|
|
// Add all options |
136
|
|
|
foreach ($this->getSourceEmpty() as $value => $title) { |
137
|
|
|
$options[] = $this->getFieldOption($value, $title); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$properties = array_merge($properties, array( |
141
|
|
|
'Options' => new ArrayList($options) |
142
|
|
|
)); |
143
|
|
|
|
144
|
|
|
return parent::Field($properties); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|