‪TYPO3CMS  ‪main
OptionViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
22 
27 {
31  protected ‪$tagName = 'option';
32 
33  public function ‪initializeArguments(): void
34  {
35  $this->registerUniversalTagAttributes();
36  $this->registerArgument('selected', 'boolean', 'If set, overrides automatic detection of selected state for this option.');
37  $this->registerArgument('additionalAttributes', 'array', 'Additional tag attributes. They will be added directly to the resulting HTML tag.');
38  $this->registerArgument('data', 'array', 'Additional data-* attributes. They will each be added with a "data-" prefix.');
39  $this->registerTagAttribute('value', 'mixed', 'Value to be inserted in HTML tag - must be convertible to string!');
40  }
41 
42  public function ‪render(): string
43  {
44  $childContent = $this->renderChildren();
45  $this->tag->setContent($childContent);
46  $value = $this->arguments['value'] ?? $childContent;
47  if ($this->arguments['selected'] ?? $this->‪isValueSelected((string)$value)) {
48  $this->tag->addAttribute('selected', 'selected');
49  }
50  $this->tag->addAttribute('value', $value);
51  $parentRequestedFormTokenFieldName = $this->renderingContext->getViewHelperVariableContainer()->get(
52  SelectViewHelper::class,
53  'registerFieldNameForFormTokenGeneration'
54  );
55  if ($parentRequestedFormTokenFieldName) {
56  // parent (select field) has requested this option must add one more
57  // entry in the token generation registry for one additional potential
58  // value of the field. Happens when "multiple" is true on parent.
59  $this->‪registerFieldNameForFormTokenGeneration($parentRequestedFormTokenFieldName);
60  }
61  return $this->tag->render();
62  }
63 
64  protected function ‪isValueSelected(string $value): bool
65  {
66  $selectedValue = $this->renderingContext->getViewHelperVariableContainer()->get(SelectViewHelper::class, 'selectedValue');
67  if (is_array($selectedValue)) {
68  return in_array($value, array_map(strval(...), $selectedValue), true);
69  }
70  if ($selectedValue instanceof \Iterator) {
71  return in_array($value, array_map(strval(...), iterator_to_array($selectedValue)), true);
72  }
73  return $value === (string)$selectedValue;
74  }
75 }
‪TYPO3\CMS\Fluid\ViewHelpers\Form\Select
Definition: OptgroupViewHelper.php:18
‪TYPO3\CMS\Fluid\ViewHelpers\Form\Select\OptionViewHelper\initializeArguments
‪initializeArguments()
Definition: OptionViewHelper.php:32
‪TYPO3\CMS\Fluid\ViewHelpers\Form\Select\OptionViewHelper
Definition: OptionViewHelper.php:27
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper\registerFieldNameForFormTokenGeneration
‪registerFieldNameForFormTokenGeneration(string $fieldName)
Definition: AbstractFormViewHelper.php:100
‪TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper
Definition: SelectViewHelper.php:97
‪TYPO3\CMS\Fluid\ViewHelpers\Form\Select\OptionViewHelper\$tagName
‪string $tagName
Definition: OptionViewHelper.php:30
‪TYPO3\CMS\Fluid\ViewHelpers\Form\Select\OptionViewHelper\isValueSelected
‪isValueSelected(string $value)
Definition: OptionViewHelper.php:63
‪TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
Definition: AbstractFormFieldViewHelper.php:37
‪TYPO3\CMS\Fluid\ViewHelpers\Form\Select\OptionViewHelper\render
‪render()
Definition: OptionViewHelper.php:41