‪TYPO3CMS  9.5
OptionViewHelper.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  */
17 
22 {
26  protected ‪$tagName = 'option';
27 
31  public function ‪initializeArguments()
32  {
33  $this->registerUniversalTagAttributes();
34  $this->registerArgument('selected', 'boolean', 'If set, overrides automatic detection of selected state for this option.');
35  $this->registerArgument('additionalAttributes', 'array', 'Additional tag attributes. They will be added directly to the resulting HTML tag.');
36  $this->registerArgument('data', 'array', 'Additional data-* attributes. They will each be added with a "data-" prefix.');
37  $this->registerTagAttribute('value', 'mixed', 'Value to be inserted in HTML tag - must be convertible to string!');
38  }
39 
43  public function ‪render()
44  {
45  if ($this->arguments['selected'] ?? $this->‪isValueSelected($this->arguments['value'])) {
46  $this->tag->addAttribute('selected', 'selected');
47  }
48  $childContent = $this->renderChildren();
49  $this->tag->setContent($childContent);
50  $value = $this->arguments['value'] ?? $childContent;
51  $this->tag->addAttribute('value', $value);
52  $parentRequestedFormTokenFieldName = $this->renderingContext->getViewHelperVariableContainer()->get(
53  SelectViewHelper::class,
54  'registerFieldNameForFormTokenGeneration'
55  );
56  if ($parentRequestedFormTokenFieldName) {
57  // parent (select field) has requested this option must add one more
58  // entry in the token generation registry for one additional potential
59  // value of the field. Happens when "multiple" is true on parent.
60  $this->‪registerFieldNameForFormTokenGeneration($parentRequestedFormTokenFieldName);
61  }
62  return $this->tag->render();
63  }
64 
69  protected function ‪isValueSelected($value)
70  {
71  $selectedValue = $this->renderingContext->getViewHelperVariableContainer()->get(SelectViewHelper::class, 'selectedValue');
72  if (is_array($selectedValue)) {
73  return in_array($value, $selectedValue);
74  }
75  if ($selectedValue instanceof \Iterator) {
76  return in_array($value, iterator_to_array($selectedValue));
77  }
78  return $value == $selectedValue;
79  }
80 }
‪TYPO3\CMS\Fluid\ViewHelpers\Form\Select\OptionViewHelper\render
‪string render()
Definition: OptionViewHelper.php:42
‪TYPO3\CMS\Fluid\ViewHelpers\Form\Select
Definition: OptgroupViewHelper.php:2
‪TYPO3\CMS\Fluid\ViewHelpers\Form\Select\OptionViewHelper\isValueSelected
‪bool isValueSelected($value)
Definition: OptionViewHelper.php:68
‪TYPO3\CMS\Fluid\ViewHelpers\Form\Select\OptionViewHelper\initializeArguments
‪initializeArguments()
Definition: OptionViewHelper.php:30
‪TYPO3\CMS\Fluid\ViewHelpers\Form\Select\OptionViewHelper
Definition: OptionViewHelper.php:22
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\registerFieldNameForFormTokenGeneration
‪registerFieldNameForFormTokenGeneration($fieldName)
Definition: AbstractFormViewHelper.php:101
‪TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper
Definition: SelectViewHelper.php:91
‪TYPO3\CMS\Fluid\ViewHelpers\Form\Select\OptionViewHelper\$tagName
‪string $tagName
Definition: OptionViewHelper.php:25
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
Definition: AbstractFormFieldViewHelper.php:26