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