[TASK] Streamline phpdoc annotations in EXT:fluid
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Form / Select / OptionViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Form\Select;
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 use TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper;
17
18 /**
19 * Adds custom `<option>` tags inside an `<f:form.select>`
20 */
21 class OptionViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
22 {
23 /**
24 * @var string
25 */
26 protected $tagName = 'option';
27
28 /**
29 * Initialize additional arguments available for this tag view helper.
30 */
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
40 /**
41 * @return string
42 */
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->viewHelperVariableContainer->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
65 /**
66 * @param mixed $value
67 * @return bool
68 */
69 protected function isValueSelected($value)
70 {
71 $selectedValue = $this->viewHelperVariableContainer->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 }