TYPO3 CMS  TYPO3_8-7
SelectTreeElement.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 
23 {
27  protected $defaultFieldWizard = [
28  'localizationStateSelector' => [
29  'renderType' => 'localizationStateSelector',
30  ],
31  ];
32 
39  protected $itemsToShow = 15;
40 
47  protected $minItemsToShow = 5;
48 
54  protected $itemHeight = 20;
55 
62  public function render()
63  {
64  $resultArray = $this->initializeResultArray();
65  $parameterArray = $this->data['parameterArray'];
66  $formElementId = md5($parameterArray['itemFormElName']);
67 
68  // Field configuration from TCA:
69  $config = $parameterArray['fieldConf']['config'];
70  $readOnly = !empty($config['readOnly']) ? 'true' : 'false';
71  $exclusiveKeys = !empty($config['exclusiveKeys']) ? $config['exclusiveKeys'] : '';
72  $exclusiveKeys = $exclusiveKeys . ',';
73  $appearance = !empty($config['treeConfig']['appearance']) ? $config['treeConfig']['appearance'] : [];
74  $expanded = !empty($appearance['expandAll']);
75  $showHeader = !empty($appearance['showHeader']);
76  if (isset($config['size']) && (int)$config['size'] > 0) {
77  $height = max($this->minItemsToShow, (int)$config['size']);
78  } else {
79  $height = $this->itemsToShow;
80  }
81  $heightInPx = $height * $this->itemHeight;
82  $treeWrapperId = 'tree_' . $formElementId;
83 
84  $fieldName = $this->data['fieldName'];
85 
86  $dataStructureIdentifier = '';
87  $flexFormSheetName = '';
88  $flexFormFieldName = '';
89  $flexFormContainerName = '';
90  $flexFormContainerIdentifier = '';
91  $flexFormContainerFieldName = '';
92  $flexFormSectionContainerIsNew = false;
93  if ($this->data['processedTca']['columns'][$fieldName]['config']['type'] === 'flex') {
94  $dataStructureIdentifier = $this->data['processedTca']['columns'][$fieldName]['config']['dataStructureIdentifier'];
95  if (isset($this->data['flexFormSheetName'])) {
96  $flexFormSheetName = $this->data['flexFormSheetName'];
97  }
98  if (isset($this->data['flexFormFieldName'])) {
99  $flexFormFieldName = $this->data['flexFormFieldName'];
100  }
101  if (isset($this->data['flexFormContainerName'])) {
102  $flexFormContainerName = $this->data['flexFormContainerName'];
103  }
104  if (isset($this->data['flexFormContainerFieldName'])) {
105  $flexFormContainerFieldName = $this->data['flexFormContainerFieldName'];
106  }
107  if (isset($this->data['flexFormContainerIdentifier'])) {
108  $flexFormContainerIdentifier = $this->data['flexFormContainerIdentifier'];
109  }
110  // Add a flag this is a tree in a new flex section container element. This is needed to initialize
111  // the databaseRow with this container again so the tree data provider is able to calculate tree items.
112  if (!empty($this->data['flexSectionContainerPreparation'])) {
113  $flexFormSectionContainerIsNew = true;
114  }
115  }
116 
117  $fieldInformationResult = $this->renderFieldInformation();
118  $fieldInformationHtml = $fieldInformationResult['html'];
119  $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
120 
121  $fieldWizardResult = $this->renderFieldWizard();
122  $fieldWizardHtml = $fieldWizardResult['html'];
123  $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
124 
125  $html = [];
126  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
127  if ($readOnly === 'false') {
128  $html[] = $fieldInformationHtml;
129  }
130  $html[] = '<div class="form-control-wrap">';
131  $html[] = '<div class="form-wizards-wrap">';
132  $html[] = '<div class="form-wizards-element">';
133  $html[] = '<div class="typo3-tceforms-tree">';
134  $html[] = '<input class="treeRecord" type="hidden"';
135  $html[] = ' data-formengine-validation-rules="' . htmlspecialchars($this->getValidationDataAsJsonString($config)) . '"';
136  $html[] = ' data-relatedfieldname="' . htmlspecialchars($parameterArray['itemFormElName']) . '"';
137  $html[] = ' data-tablename="' . htmlspecialchars($this->data['tableName']) . '"';
138  $html[] = ' data-fieldname="' . htmlspecialchars($this->data['fieldName']) . '"';
139  $html[] = ' data-uid="' . (int)$this->data['vanillaUid'] . '"';
140  $html[] = ' data-recordtypevalue="' . htmlspecialchars($this->data['recordTypeValue']) . '"';
141  $html[] = ' data-datastructureidentifier="' . htmlspecialchars($dataStructureIdentifier) . '"';
142  $html[] = ' data-flexformsheetname="' . htmlspecialchars($flexFormSheetName) . '"';
143  $html[] = ' data-flexformfieldname="' . htmlspecialchars($flexFormFieldName) . '"';
144  $html[] = ' data-flexformcontainername="' . htmlspecialchars($flexFormContainerName) . '"';
145  $html[] = ' data-flexformcontaineridentifier="' . htmlspecialchars($flexFormContainerIdentifier) . '"';
146  $html[] = ' data-flexformcontainerfieldname="' . htmlspecialchars($flexFormContainerFieldName) . '"';
147  $html[] = ' data-flexformsectioncontainerisnew="' . htmlspecialchars($flexFormSectionContainerIsNew) . '"';
148  $html[] = ' data-command="' . htmlspecialchars($this->data['command']) . '"';
149  $html[] = ' data-read-only="' . $readOnly . '"';
150  $html[] = ' data-tree-exclusive-keys="' . htmlspecialchars($exclusiveKeys) . '"';
151  $html[] = ' data-tree-expand-up-to-level="' . ($expanded ? '999' : '1') . '"';
152  $html[] = ' data-tree-show-toolbar="' . $showHeader . '"';
153  $html[] = ' name="' . htmlspecialchars($parameterArray['itemFormElName']) . '"';
154  $html[] = ' id="treeinput' . $formElementId . '"';
155  $html[] = ' value="' . htmlspecialchars(implode(',', $parameterArray['itemFormElValue'])) . '"';
156  $html[] = '/>';
157  $html[] = '</div>';
158  $html[] = '<div id="' . $treeWrapperId . '" class="svg-tree-wrapper" style="height: ' . $heightInPx . 'px;"></div>';
159  $html[] = '<script type="text/javascript">var ' . $treeWrapperId . ' = ' . $this->getTreeOnChangeJs() . '</script>';
160  $html[] = '</div>';
161  if ($readOnly === 'false') {
162  $html[] = '<div class="form-wizards-items-bottom">';
163  $html[] = $fieldWizardHtml;
164  $html[] = '</div>';
165  }
166  $html[] = '</div>';
167  $html[] = '</div>';
168  $html[] = '</div>';
169 
170  $resultArray['html'] = implode(LF, $html);
171 
172  // add necessary labels for tree header
173  if ($showHeader) {
174  $resultArray['additionalInlineLanguageLabelFiles'][] = 'EXT:lang/Resources/Private/Language/locallang_csh_corebe.xlf';
175  }
176  $resultArray['requireJsModules']['selectTreeElement'] = [
177  'TYPO3/CMS/Backend/FormEngine/Element/SelectTreeElement' => 'function (SelectTreeElement) { SelectTreeElement.initialize(); }'
178  ];
179 
180  return $resultArray;
181  }
182 
188  protected function getTreeOnChangeJs()
189  {
190  $parameterArray = $this->data['parameterArray'];
191  $onChange = !empty($parameterArray['fieldChangeFunc']['TBE_EDITOR_fieldChanged']) ? $parameterArray['fieldChangeFunc']['TBE_EDITOR_fieldChanged'] : '';
192  $onChange .= !empty($parameterArray['fieldChangeFunc']['alert']) ? $parameterArray['fieldChangeFunc']['alert'] : '';
193  return 'function () {' . $onChange . '}';
194  }
195 }
mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)