‪TYPO3CMS  10.4
SelectViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
19 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
20 
95 {
99  protected ‪$tagName = 'select';
100 
104  protected ‪$selectedValue;
105 
109  public function ‪initializeArguments()
110  {
111  parent::initializeArguments();
112  $this->registerUniversalTagAttributes();
113  $this->registerTagAttribute('size', 'string', 'Size of input field');
114  $this->registerTagAttribute('disabled', 'string', 'Specifies that the input element should be disabled when the page loads');
115  $this->registerArgument('options', 'array', 'Associative array with internal IDs as key, and the values are displayed in the select box. Can be combined with or replaced by child f:form.select.* nodes.');
116  $this->registerArgument('optionsAfterContent', 'boolean', 'If true, places auto-generated option tags after those rendered in the tag content. If false, automatic options come first.', false, false);
117  $this->registerArgument('optionValueField', 'string', 'If specified, will call the appropriate getter on each object to determine the value.');
118  $this->registerArgument('optionLabelField', 'string', 'If specified, will call the appropriate getter on each object to determine the label.');
119  $this->registerArgument('sortByOptionLabel', 'boolean', 'If true, List will be sorted by label.', false, false);
120  $this->registerArgument('selectAllByDefault', 'boolean', 'If specified options are selected if none was set before.', false, false);
121  $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this ViewHelper', false, 'f3-form-error');
122  $this->registerArgument('prependOptionLabel', 'string', 'If specified, will provide an option at first position with the specified label.');
123  $this->registerArgument('prependOptionValue', 'string', 'If specified, will provide an option at first position with the specified value.');
124  $this->registerArgument('multiple', 'boolean', 'If set multiple options may be selected.', false, false);
125  $this->registerArgument('required', 'boolean', 'If set no empty value is allowed.', false, false);
126  }
127 
133  public function ‪render()
134  {
135  if (isset($this->arguments['required']) && $this->arguments['required']) {
136  $this->tag->addAttribute('required', 'required');
137  }
138  $name = $this->‪getName();
139  if (isset($this->arguments['multiple']) && $this->arguments['multiple']) {
140  $this->tag->addAttribute('multiple', 'multiple');
141  $name .= '[]';
142  }
143  $this->tag->addAttribute('name', $name);
144  $options = $this->‪getOptions();
145 
146  $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
147 
149  $this->‪setErrorClassAttribute();
150  $content = '';
151 
152  // register field name for token generation.
154  // in case it is a multi-select, we need to register the field name
155  // as often as there are elements in the box
156  if (isset($this->arguments['multiple']) && $this->arguments['multiple']) {
157  $content .= $this->‪renderHiddenFieldForEmptyValue();
158  // Register the field name additional times as required by the total number of
159  // options. Since we already registered it once above, we start the counter at 1
160  // instead of 0.
161  $optionsCount = count($options);
162  for ($i = 1; $i < $optionsCount; $i++) {
164  }
165  // save the parent field name so that any child f:form.select.option
166  // tag will know to call registerFieldNameForFormTokenGeneration
167  // this is the reason why "self::class" is used instead of static::class (no LSB)
168  $viewHelperVariableContainer->addOrUpdate(
169  self::class,
170  'registerFieldNameForFormTokenGeneration',
171  $name
172  );
173  }
174 
175  $viewHelperVariableContainer->addOrUpdate(self::class, 'selectedValue', $this->‪getSelectedValue());
176  $prependContent = $this->‪renderPrependOptionTag();
177  $tagContent = $this->‪renderOptionTags($options);
178  $childContent = $this->renderChildren();
179  $viewHelperVariableContainer->remove(self::class, 'selectedValue');
180  $viewHelperVariableContainer->remove(self::class, 'registerFieldNameForFormTokenGeneration');
181  if (isset($this->arguments['optionsAfterContent']) && $this->arguments['optionsAfterContent']) {
182  $tagContent = $childContent . $tagContent;
183  } else {
184  $tagContent .= $childContent;
185  }
186  $tagContent = $prependContent . $tagContent;
187 
188  $this->tag->forceClosingTag(true);
189  $this->tag->setContent($tagContent);
190  $content .= $this->tag->render();
191  return $content;
192  }
193 
199  protected function ‪renderPrependOptionTag()
200  {
201  ‪$output = '';
202  if ($this->hasArgument('prependOptionLabel')) {
203  $value = $this->hasArgument('prependOptionValue') ? $this->arguments['prependOptionValue'] : '';
204  $label = $this->arguments['prependOptionLabel'];
205  ‪$output .= $this->‪renderOptionTag($value, $label, false) . LF;
206  }
207  return ‪$output;
208  }
209 
216  protected function ‪renderOptionTags($options)
217  {
218  ‪$output = '';
219  foreach ($options as $value => $label) {
220  $isSelected = $this->‪isSelected($value);
221  ‪$output .= $this->‪renderOptionTag($value, $label, $isSelected) . LF;
222  }
223  return ‪$output;
224  }
225 
231  protected function ‪getOptions()
232  {
233  if (!is_array($this->arguments['options']) && !$this->arguments['options'] instanceof \Traversable) {
234  return [];
235  }
236  $options = [];
237  $optionsArgument = $this->arguments['options'];
238  foreach ($optionsArgument as $key => $value) {
239  if (is_object($value) || is_array($value)) {
240  if ($this->hasArgument('optionValueField')) {
241  $key = ‪ObjectAccess::getPropertyPath($value, $this->arguments['optionValueField']);
242  if (is_object($key)) {
243  if (method_exists($key, '__toString')) {
244  $key = (string)$key;
245  } else {
246  throw new Exception('Identifying value for object of class "' . (is_object($value) ? get_class($value) : gettype($value)) . '" was an object.', 1247827428);
247  }
248  }
249  } elseif ($this->persistenceManager->getIdentifierByObject($value) !== null) {
250  // @todo use $this->persistenceManager->isNewObject() once it is implemented
251  $key = $this->persistenceManager->getIdentifierByObject($value);
252  } elseif (is_object($value) && method_exists($value, '__toString')) {
253  $key = (string)$value;
254  } elseif (is_object($value)) {
255  throw new Exception('No identifying value for object of class "' . get_class($value) . '" found.', 1247826696);
256  }
257  if ($this->hasArgument('optionLabelField')) {
258  $value = ‪ObjectAccess::getPropertyPath($value, $this->arguments['optionLabelField']);
259  if (is_object($value)) {
260  if (method_exists($value, '__toString')) {
261  $value = (string)$value;
262  } else {
263  throw new Exception('Label value for object of class "' . get_class($value) . '" was an object without a __toString() method.', 1247827553);
264  }
265  }
266  } elseif (is_object($value) && method_exists($value, '__toString')) {
267  $value = (string)$value;
268  } elseif ($this->persistenceManager->getIdentifierByObject($value) !== null) {
269  // @todo use $this->persistenceManager->isNewObject() once it is implemented
270  $value = $this->persistenceManager->getIdentifierByObject($value);
271  }
272  }
273  $options[$key] = $value;
274  }
275  if ($this->arguments['sortByOptionLabel']) {
276  asort($options, SORT_LOCALE_STRING);
277  }
278  return $options;
279  }
280 
287  protected function ‪isSelected($value)
288  {
290  if ($value === ‪$selectedValue || (string)$value === ‪$selectedValue) {
291  return true;
292  }
293  if ($this->hasArgument('multiple')) {
294  if (‪$selectedValue === null && $this->arguments['selectAllByDefault'] === true) {
295  return true;
296  }
297  if (is_array(‪$selectedValue) && in_array($value, ‪$selectedValue)) {
298  return true;
299  }
300  }
301  return false;
302  }
303 
309  protected function ‪getSelectedValue()
310  {
311  $this->‪setRespectSubmittedDataValue(true);
312  $value = $this->‪getValueAttribute();
313  if (!is_array($value) && !$value instanceof \Traversable) {
314  return $this->‪getOptionValueScalar($value);
315  }
316  $selectedValues = [];
317  foreach ($value as $selectedValueElement) {
318  $selectedValues[] = $this->‪getOptionValueScalar($selectedValueElement);
319  }
320  return $selectedValues;
321  }
322 
329  protected function ‪getOptionValueScalar($valueElement)
330  {
331  if (is_object($valueElement)) {
332  if ($this->hasArgument('optionValueField')) {
333  return ‪ObjectAccess::getPropertyPath($valueElement, $this->arguments['optionValueField']);
334  }
335  // @todo use $this->persistenceManager->isNewObject() once it is implemented
336  if ($this->persistenceManager->getIdentifierByObject($valueElement) !== null) {
337  return $this->persistenceManager->getIdentifierByObject($valueElement);
338  }
339  return (string)$valueElement;
340  }
341  return $valueElement;
342  }
343 
352  protected function ‪renderOptionTag($value, $label, $isSelected)
353  {
354  ‪$output = '<option value="' . htmlspecialchars($value) . '"';
355  if ($isSelected) {
356  ‪$output .= ' selected="selected"';
357  }
358  ‪$output .= '>' . htmlspecialchars($label) . '</option>';
359  return ‪$output;
360  }
361 }
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\setErrorClassAttribute
‪setErrorClassAttribute()
Definition: AbstractFormFieldViewHelper.php:331
‪TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper\$selectedValue
‪mixed $selectedValue
Definition: SelectViewHelper.php:102
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\setRespectSubmittedDataValue
‪setRespectSubmittedDataValue($respectSubmittedDataValue)
Definition: AbstractFormFieldViewHelper.php:77
‪TYPO3\CMS\Fluid\ViewHelpers\Form
Definition: AbstractFormFieldViewHelper.php:16
‪TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper\initializeArguments
‪initializeArguments()
Definition: SelectViewHelper.php:107
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess
Definition: ObjectAccess.php:38
‪TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper\$tagName
‪string $tagName
Definition: SelectViewHelper.php:98
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\registerFieldNameForFormTokenGeneration
‪registerFieldNameForFormTokenGeneration($fieldName)
Definition: AbstractFormViewHelper.php:106
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\renderHiddenFieldForEmptyValue
‪string renderHiddenFieldForEmptyValue()
Definition: AbstractFormFieldViewHelper.php:374
‪TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper
Definition: SelectViewHelper.php:95
‪TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper\renderOptionTags
‪string renderOptionTags($options)
Definition: SelectViewHelper.php:214
‪TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper\getOptions
‪array getOptions()
Definition: SelectViewHelper.php:229
‪TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper\render
‪string render()
Definition: SelectViewHelper.php:131
‪$output
‪$output
Definition: annotationChecker.php:119
‪TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper\renderPrependOptionTag
‪string renderPrependOptionTag()
Definition: SelectViewHelper.php:197
‪TYPO3\CMS\Extbase\Reflection\ObjectAccess\getPropertyPath
‪static mixed getPropertyPath($subject, string $propertyPath)
Definition: ObjectAccess.php:139
‪TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper\getOptionValueScalar
‪string getOptionValueScalar($valueElement)
Definition: SelectViewHelper.php:327
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
Definition: AbstractFormFieldViewHelper.php:30
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\addAdditionalIdentityPropertiesIfNeeded
‪addAdditionalIdentityPropertiesIfNeeded()
Definition: AbstractFormFieldViewHelper.php:243
‪TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper\isSelected
‪bool isSelected($value)
Definition: SelectViewHelper.php:285
‪TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper\getSelectedValue
‪mixed getSelectedValue()
Definition: SelectViewHelper.php:307
‪TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper\renderOptionTag
‪string renderOptionTag($value, $label, $isSelected)
Definition: SelectViewHelper.php:350
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getName
‪string getName()
Definition: AbstractFormFieldViewHelper.php:90
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper\getValueAttribute
‪mixed getValueAttribute()
Definition: AbstractFormFieldViewHelper.php:151