[FEATURE] Introduce prepend option for Select ViewHelper
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Form / SelectViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Form;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13 /**
14 * This view helper generates a <select> dropdown list for the use with a form.
15 *
16 * = Basic usage =
17 *
18 * The most straightforward way is to supply an associative array as the "options" parameter.
19 * The array key is used as option key, and the value is used as human-readable name.
20 *
21 * <code title="Basic usage">
22 * <f:form.select name="paymentOptions" options="{payPal: 'PayPal International Services', visa: 'VISA Card'}" />
23 * </code>
24 *
25 * = Pre-select a value =
26 *
27 * To pre-select a value, set "value" to the option key which should be selected.
28 * <code title="Default value">
29 * <f:form.select name="paymentOptions" options="{payPal: 'PayPal International Services', visa: 'VISA Card'}" value="visa" />
30 * </code>
31 * Generates a dropdown box like above, except that "VISA Card" is selected.
32 *
33 * If the select box is a multi-select box (multiple="true"), then "value" can be an array as well.
34 *
35 * = Usage on domain objects =
36 *
37 * If you want to output domain objects, you can just pass them as array into the "options" parameter.
38 * To define what domain object value should be used as option key, use the "optionValueField" variable. Same goes for optionLabelField.
39 * If neither is given, the Identifier (UID/uid) and the __toString() method are tried as fallbacks.
40 *
41 * If the optionValueField variable is set, the getter named after that value is used to retrieve the option key.
42 * If the optionLabelField variable is set, the getter named after that value is used to retrieve the option value.
43 *
44 * If the prependOptionLabel variable is set, an option item is added in first position, bearing an empty string or -
45 * If provided, the value of the prependOptionValue variable as value.
46 *
47 * <code title="Domain objects">
48 * <f:form.select name="users" options="{userArray}" optionValueField="id" optionLabelField="firstName" />
49 * </code>
50 * In the above example, the userArray is an array of "User" domain objects, with no array key specified.
51 *
52 * So, in the above example, the method $user->getId() is called to retrieve the key, and $user->getFirstName() to retrieve the displayed value of each entry.
53 *
54 * The "value" property now expects a domain object, and tests for object equivalence.
55 *
56 * @api
57 */
58 class SelectViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper {
59
60 /**
61 * @var string
62 */
63 protected $tagName = 'select';
64
65 /**
66 * @var mixed
67 */
68 protected $selectedValue = NULL;
69
70 /**
71 * Initialize arguments.
72 *
73 * @return void
74 * @api
75 */
76 public function initializeArguments() {
77 parent::initializeArguments();
78 $this->registerUniversalTagAttributes();
79 $this->registerTagAttribute('multiple', 'string', 'if set, multiple select field');
80 $this->registerTagAttribute('size', 'string', 'Size of input field');
81 $this->registerTagAttribute('disabled', 'string', 'Specifies that the input element should be disabled when the page loads');
82 $this->registerArgument('options', 'array', 'Associative array with internal IDs as key, and the values are displayed in the select box', TRUE);
83 $this->registerArgument('optionValueField', 'string', 'If specified, will call the appropriate getter on each object to determine the value.');
84 $this->registerArgument('optionLabelField', 'string', 'If specified, will call the appropriate getter on each object to determine the label.');
85 $this->registerArgument('sortByOptionLabel', 'boolean', 'If true, List will be sorted by label.', FALSE, FALSE);
86 $this->registerArgument('selectAllByDefault', 'boolean', 'If specified options are selected if none was set before.', FALSE, FALSE);
87 $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this view helper', FALSE, 'f3-form-error');
88 $this->registerArgument('prependOptionLabel', 'string', 'If specified, will provide an option at first position with the specified label.');
89 $this->registerArgument('prependOptionValue', 'string', 'If specified, will provide an option at first position with the specified value.');
90 }
91
92 /**
93 * Render the tag.
94 *
95 * @return string rendered tag.
96 * @api
97 */
98 public function render() {
99 $name = $this->getName();
100 if ($this->hasArgument('multiple')) {
101 $name .= '[]';
102 }
103 $this->tag->addAttribute('name', $name);
104 $options = $this->getOptions();
105 if (empty($options)) {
106 $options = array('' => '');
107 }
108 $this->tag->setContent($this->renderOptionTags($options));
109 $this->setErrorClassAttribute();
110 $content = '';
111 // register field name for token generation.
112 // in case it is a multi-select, we need to register the field name
113 // as often as there are elements in the box
114 if ($this->hasArgument('multiple') && $this->arguments['multiple'] !== '') {
115 $content .= $this->renderHiddenFieldForEmptyValue();
116 for ($i = 0; $i < count($options); $i++) {
117 $this->registerFieldNameForFormTokenGeneration($name);
118 }
119 } else {
120 $this->registerFieldNameForFormTokenGeneration($name);
121 }
122 $content .= $this->tag->render();
123 return $content;
124 }
125
126 /**
127 * Render the option tags.
128 *
129 * @param array $options the options for the form.
130 * @return string rendered tags.
131 */
132 protected function renderOptionTags($options) {
133 $output = '';
134 if ($this->hasArgument('prependOptionLabel')) {
135 $value = $this->hasArgument('prependOptionValue') ? $this->arguments['prependOptionValue'] : '';
136 $label = $this->arguments['prependOptionLabel'];
137 $output .= $this->renderOptionTag($value, $label, FALSE) . chr(10);
138 }
139 foreach ($options as $value => $label) {
140 $isSelected = $this->isSelected($value);
141 $output .= $this->renderOptionTag($value, $label, $isSelected) . chr(10);
142 }
143 return $output;
144 }
145
146 /**
147 * Render the option tags.
148 *
149 * @return array an associative array of options, key will be the value of the option tag
150 */
151 protected function getOptions() {
152 if (!is_array($this->arguments['options']) && !$this->arguments['options'] instanceof \Traversable) {
153 return array();
154 }
155 $options = array();
156 $optionsArgument = $this->arguments['options'];
157 foreach ($optionsArgument as $key => $value) {
158 if (is_object($value)) {
159 if ($this->hasArgument('optionValueField')) {
160 $key = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($value, $this->arguments['optionValueField']);
161 if (is_object($key)) {
162 if (method_exists($key, '__toString')) {
163 $key = (string) $key;
164 } else {
165 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('Identifying value for object of class "' . get_class($value) . '" was an object.', 1247827428);
166 }
167 }
168 // TODO: use $this->persistenceManager->isNewObject() once it is implemented
169 } elseif ($this->persistenceManager->getIdentifierByObject($value) !== NULL) {
170 $key = $this->persistenceManager->getIdentifierByObject($value);
171 } elseif (method_exists($value, '__toString')) {
172 $key = (string) $value;
173 } else {
174 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('No identifying value for object of class "' . get_class($value) . '" found.', 1247826696);
175 }
176 if ($this->hasArgument('optionLabelField')) {
177 $value = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($value, $this->arguments['optionLabelField']);
178 if (is_object($value)) {
179 if (method_exists($value, '__toString')) {
180 $value = (string) $value;
181 } else {
182 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('Label value for object of class "' . get_class($value) . '" was an object without a __toString() method.', 1247827553);
183 }
184 }
185 } elseif (method_exists($value, '__toString')) {
186 $value = (string) $value;
187 // TODO: use $this->persistenceManager->isNewObject() once it is implemented
188 } elseif ($this->persistenceManager->getIdentifierByObject($value) !== NULL) {
189 $value = $this->persistenceManager->getIdentifierByObject($value);
190 }
191 }
192 $options[$key] = $value;
193 }
194 if ($this->arguments['sortByOptionLabel']) {
195 asort($options, SORT_LOCALE_STRING);
196 }
197 return $options;
198 }
199
200 /**
201 * Render the option tags.
202 *
203 * @param mixed $value Value to check for
204 * @return boolean TRUE if the value should be marked a s selected; FALSE otherwise
205 */
206 protected function isSelected($value) {
207 $selectedValue = $this->getSelectedValue();
208 if ($value === $selectedValue || (string) $value === $selectedValue) {
209 return TRUE;
210 }
211 if ($this->hasArgument('multiple')) {
212 if (is_null($selectedValue) && $this->arguments['selectAllByDefault'] === TRUE) {
213 return TRUE;
214 } elseif (is_array($selectedValue) && in_array($value, $selectedValue)) {
215 return TRUE;
216 }
217 }
218 return FALSE;
219 }
220
221 /**
222 * Retrieves the selected value(s)
223 *
224 * @return mixed value string or an array of strings
225 */
226 protected function getSelectedValue() {
227 $value = $this->getValue();
228 if (!is_array($value) && !$value instanceof \Traversable) {
229 return $this->getOptionValueScalar($value);
230 }
231 $selectedValues = array();
232 foreach ($value as $selectedValueElement) {
233 $selectedValues[] = $this->getOptionValueScalar($selectedValueElement);
234 }
235 return $selectedValues;
236 }
237
238 /**
239 * Get the option value for an object
240 *
241 * @param mixed $valueElement
242 * @return string
243 */
244 protected function getOptionValueScalar($valueElement) {
245 if (is_object($valueElement)) {
246 if ($this->hasArgument('optionValueField')) {
247 return \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($valueElement, $this->arguments['optionValueField']);
248 } else {
249 // TODO: use $this->persistenceManager->isNewObject() once it is implemented
250 if ($this->persistenceManager->getIdentifierByObject($valueElement) !== NULL) {
251 return $this->persistenceManager->getIdentifierByObject($valueElement);
252 } else {
253 return (string) $valueElement;
254 }
255 }
256 } else {
257 return $valueElement;
258 }
259 }
260
261 /**
262 * Render one option tag
263 *
264 * @param string $value value attribute of the option tag (will be escaped)
265 * @param string $label content of the option tag (will be escaped)
266 * @param boolean $isSelected specifies wheter or not to add selected attribute
267 * @return string the rendered option tag
268 */
269 protected function renderOptionTag($value, $label, $isSelected) {
270 $output = '<option value="' . htmlspecialchars($value) . '"';
271 if ($isSelected) {
272 $output .= ' selected="selected"';
273 }
274 $output .= '>' . htmlspecialchars($label) . '</option>';
275 return $output;
276 }
277 }
278
279 ?>