[TASK] Use null coalescing operator where possible
[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 * @api
22 */
23 class OptionViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper
24 {
25 /**
26 * @var string
27 */
28 protected $tagName = 'option';
29
30 /**
31 * Initialize additional arguments available for this tag view helper.
32 */
33 public function initializeArguments()
34 {
35 $this->registerUniversalTagAttributes();
36 $this->registerArgument('additionalAttributes', 'array', 'Additional tag attributes. They will be added directly to the resulting HTML tag.');
37 $this->registerArgument('data', 'array', 'Additional data-* attributes. They will each be added with a "data-" prefix.');
38 $this->registerTagAttribute('value', 'mixed', 'Value to be inserted in HTML tag - must be convertible to string!');
39 $this->registerTagAttribute('selected', 'boolean', 'If filled, overrides automatic detection of selected state for this option');
40 }
41
42 /**
43 * @return string
44 */
45 public function render()
46 {
47 if ($this->arguments['selected'] === null) {
48 // user did not provide a tag attribute value. Determine if we need to
49 // set this attribute - or remove it entirely to prevent an empty attribute.
50 if ($this->isValueSelected($this->arguments['value'])) {
51 $this->tag->addAttribute('selected', 'selected');
52 } else {
53 $this->tag->removeAttribute('selected');
54 }
55 }
56 $childContent = $this->renderChildren();
57 $this->tag->setContent($childContent);
58 $value = $this->arguments['value'] ?? $childContent;
59 $this->tag->addAttribute('value', $value);
60 $parentRequestedFormTokenFieldName = $this->viewHelperVariableContainer->get(
61 SelectViewHelper::class,
62 'registerFieldNameForFormTokenGeneration'
63 );
64 if ($parentRequestedFormTokenFieldName) {
65 // parent (select field) has requested this option must add one more
66 // entry in the token generation registry for one additional potential
67 // value of the field. Happens when "multiple" is true on parent.
68 $this->registerFieldNameForFormTokenGeneration($parentRequestedFormTokenFieldName);
69 }
70 return $this->tag->render();
71 }
72
73 /**
74 * @param mixed $value
75 * @return bool
76 */
77 protected function isValueSelected($value)
78 {
79 $selectedValue = $this->viewHelperVariableContainer->get(SelectViewHelper::class, 'selectedValue');
80 if (is_array($selectedValue)) {
81 return in_array($value, $selectedValue);
82 }
83 if ($selectedValue instanceof \Iterator) {
84 return in_array($value, iterator_to_array($selectedValue));
85 }
86 return $value == $selectedValue;
87 }
88 }