TYPO3 CMS  TYPO3_7-6
AggregateSelectOptionsViewHelper.php
Go to the documentation of this file.
1 <?php
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 
19 
24 {
28  protected $options = [];
29 
33  protected $selectedValues = [];
34 
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 
58  protected function createElement(Element $model, array $optGroupData = [])
59  {
60  $this->checkElementForOptgroup($model, $optGroupData);
61  }
62 
68  protected function checkElementForOptgroup(Element $model, array $optGroupData = [])
69  {
70  if ($model->getElementType() === 'OPTGROUP') {
71  $optGroupData = [
72  'label' => $model->getAdditionalArgument('label'),
73  'disabled' => $model->getAdditionalArgument('disabled')
74  ];
75  $this->getChildElements($model, $optGroupData);
76  } else {
77  $optionData = [
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 
102  protected function getChildElements(Element $model, array $optGroupData = [])
103  {
104  foreach ($model->getChildElements() as $element) {
105  $this->createElement($element, $optGroupData);
106  }
107  }
108 }