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