[BUGFIX] EXT:form - Selected attribute not rendered
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / ViewHelpers / AggregateSelectOptionsViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Form\ViewHelpers;
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
17 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
18 use TYPO3\CMS\Form\Domain\Model\Element;
19
20 /**
21 * Aggregator for the select options
22 */
23 class AggregateSelectOptionsViewHelper extends AbstractViewHelper
24 {
25 /**
26 * @var array
27 */
28 protected $options = array();
29
30 /**
31 * @var array
32 */
33 protected $selectedValues = array();
34
35 /**
36 * @param Element $model
37 * @param boolean $returnSelectedValues
38 * @return array
39 */
40 public function render(Element $model, $returnSelectedValues = false)
41 {
42 foreach ($model->getChildElements() as $element) {
43 $this->createElement($element);
44 }
45
46 if ($returnSelectedValues === true) {
47 return $this->selectedValues;
48 }
49
50 return $this->options;
51 }
52
53 /**
54 * @param Element $model
55 * @param array $optGroupData
56 * @return void
57 */
58 protected function createElement(Element $model, array $optGroupData = array())
59 {
60 $this->checkElementForOptgroup($model, $optGroupData);
61 }
62
63 /**
64 * @param Element $model
65 * @param array $optGroupData
66 * @return void
67 */
68 protected function checkElementForOptgroup(Element $model, array $optGroupData = array())
69 {
70 if ($model->getElementType() === 'OPTGROUP') {
71 $optGroupData = array(
72 'label' => $model->getAdditionalArgument('label'),
73 'disabled' => $model->getAdditionalArgument('disabled')
74 );
75 $this->getChildElements($model, $optGroupData);
76 } else {
77 $optionData = array(
78 'value' => $model->getAdditionalArgument('value') ?: $model->getElementCounter(),
79 'label' => $model->getAdditionalArgument('text'),
80 'selected' => $model->getAdditionalArgument('selected'),
81 );
82
83 if (!empty($optionData['selected'])) {
84 $this->selectedValues[] = $optionData['value'];
85 }
86
87 if (count($optGroupData)) {
88 $optGroupLabel = $optGroupData['label'];
89 $this->options[$optGroupLabel]['disabled'] = $optGroupData['disabled'];
90 $this->options[$optGroupLabel]['options'][] = $optionData;
91 } else {
92 $this->options[] = $optionData;
93 }
94 }
95 }
96
97 /**
98 * @param Element $model
99 * @param array $optGroupData
100 * @return void
101 */
102 protected function getChildElements(Element $model, array $optGroupData = array())
103 {
104 foreach ($model->getChildElements() as $element) {
105 $this->createElement($element, $optGroupData);
106 }
107 }
108 }