‪TYPO3CMS  11.5
SelectTreeElement.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use TYPO3\CMS\Backend\Form\Behavior\OnFieldChangeInterface;
19 use TYPO3\CMS\Backend\Form\Behavior\OnFieldChangeTrait;
22 
29 {
30  use OnFieldChangeTrait;
31 
37  protected ‪$defaultFieldInformation = [
38  'tcaDescription' => [
39  'renderType' => 'tcaDescription',
40  ],
41  ];
42 
46  protected ‪$defaultFieldWizard = [
47  'localizationStateSelector' => [
48  'renderType' => 'localizationStateSelector',
49  ],
50  ];
51 
58  protected ‪$itemsToShow = 15;
59 
66  protected ‪$minItemsToShow = 5;
67 
73  protected ‪$itemHeight = 20;
74 
81  public function ‪render()
82  {
83  $resultArray = $this->‪initializeResultArray();
84  $parameterArray = $this->data['parameterArray'];
85  $formElementId = md5($parameterArray['itemFormElName']);
86 
87  // Field configuration from TCA:
88  $config = $parameterArray['fieldConf']['config'];
89  $readOnly = !empty($config['readOnly']);
90  $exclusiveKeys = !empty($config['exclusiveKeys']) ? $config['exclusiveKeys'] : '';
91  $exclusiveKeys = $exclusiveKeys . ',';
92  $appearance = !empty($config['treeConfig']['appearance']) ? $config['treeConfig']['appearance'] : [];
93  $expanded = !empty($appearance['expandAll']);
94  $showHeader = !empty($appearance['showHeader']);
95  if (isset($config['size']) && (int)$config['size'] > 0) {
96  $height = max($this->minItemsToShow, (int)$config['size']);
97  } else {
98  $height = ‪$this->itemsToShow;
99  }
100  $heightInPx = $height * ‪$this->itemHeight;
101  $treeWrapperId = 'tree_' . $formElementId;
102  $fieldId = 'tree_record_' . $formElementId;
103 
104  $fieldName = $this->data['fieldName'];
105 
106  $dataStructureIdentifier = '';
107  $flexFormSheetName = '';
108  $flexFormFieldName = '';
109  $flexFormContainerName = '';
110  $flexFormContainerIdentifier = '';
111  $flexFormContainerFieldName = '';
112  $flexFormSectionContainerIsNew = false;
113  if ($this->data['processedTca']['columns'][$fieldName]['config']['type'] === 'flex') {
114  $dataStructureIdentifier = $this->data['processedTca']['columns'][$fieldName]['config']['dataStructureIdentifier'];
115  if (isset($this->data['flexFormSheetName'])) {
116  $flexFormSheetName = $this->data['flexFormSheetName'];
117  }
118  if (isset($this->data['flexFormFieldName'])) {
119  $flexFormFieldName = $this->data['flexFormFieldName'];
120  }
121  if (isset($this->data['flexFormContainerName'])) {
122  $flexFormContainerName = $this->data['flexFormContainerName'];
123  }
124  if (isset($this->data['flexFormContainerFieldName'])) {
125  $flexFormContainerFieldName = $this->data['flexFormContainerFieldName'];
126  }
127  if (isset($this->data['flexFormContainerIdentifier'])) {
128  $flexFormContainerIdentifier = $this->data['flexFormContainerIdentifier'];
129  }
130  // Add a flag this is a tree in a new flex section container element. This is needed to initialize
131  // the databaseRow with this container again so the tree data provider is able to calculate tree items.
132  if (!empty($this->data['flexSectionContainerPreparation'])) {
133  $flexFormSectionContainerIsNew = true;
134  }
135  }
136 
137  $fieldInformationResult = $this->‪renderFieldInformation();
138  $fieldInformationHtml = $fieldInformationResult['html'];
139  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
140 
141  $fieldWizardResult = $this->‪renderFieldWizard();
142  $fieldWizardHtml = $fieldWizardResult['html'];
143  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
144 
145  $html = [];
146  $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
147  $html[] = $fieldInformationHtml;
148  $html[] = '<div class="form-control-wrap">';
149  $html[] = '<div class="form-wizards-wrap">';
150  $html[] = '<div class="form-wizards-element">';
151  $html[] = '<div class="typo3-tceforms-tree">';
152  $html[] = '<input class="treeRecord" type="hidden" id="' . htmlspecialchars($fieldId) . '"';
153  $html[] = ' data-formengine-validation-rules="' . htmlspecialchars($this->‪getValidationDataAsJsonString($config)) . '"';
154  $html[] = ' data-relatedfieldname="' . htmlspecialchars($parameterArray['itemFormElName']) . '"';
155  $html[] = ' data-tablename="' . htmlspecialchars($this->data['tableName']) . '"';
156  $html[] = ' data-fieldname="' . htmlspecialchars($this->data['fieldName']) . '"';
157  $html[] = ' data-uid="' . (int)$this->data['vanillaUid'] . '"';
158  $html[] = ' data-recordtypevalue="' . htmlspecialchars($this->data['recordTypeValue']) . '"';
159  $html[] = ' data-datastructureidentifier="' . htmlspecialchars($dataStructureIdentifier) . '"';
160  $html[] = ' data-flexformsheetname="' . htmlspecialchars($flexFormSheetName) . '"';
161  $html[] = ' data-flexformfieldname="' . htmlspecialchars($flexFormFieldName) . '"';
162  $html[] = ' data-flexformcontainername="' . htmlspecialchars($flexFormContainerName) . '"';
163  $html[] = ' data-flexformcontaineridentifier="' . htmlspecialchars($flexFormContainerIdentifier) . '"';
164  $html[] = ' data-flexformcontainerfieldname="' . htmlspecialchars($flexFormContainerFieldName) . '"';
165  $html[] = ' data-flexformsectioncontainerisnew="' . htmlspecialchars((string)$flexFormSectionContainerIsNew) . '"';
166  $html[] = ' data-command="' . htmlspecialchars($this->data['command']) . '"';
167  $html[] = ' data-read-only="' . ($readOnly ? '1' : '0') . '"';
168  $html[] = ' data-tree-exclusive-keys="' . htmlspecialchars($exclusiveKeys) . '"';
169  $html[] = ' data-tree-expand-up-to-level="' . ($expanded ? '999' : '1') . '"';
170  $html[] = ' data-tree-show-toolbar="' . $showHeader . '"';
171  $html[] = ' name="' . htmlspecialchars($parameterArray['itemFormElName']) . '"';
172  $html[] = ' id="treeinput' . $formElementId . '"';
173  $html[] = ' value="' . htmlspecialchars(implode(',', $parameterArray['itemFormElValue'])) . '"';
174  $html[] = ' data-overridevalues="' . GeneralUtility::jsonEncodeForHtmlAttribute($this->data['overrideValues']) . '"';
175  $html[] = ' data-defaultvalues="' . GeneralUtility::jsonEncodeForHtmlAttribute($this->data['defaultValues']) . '"';
176  $html[] = '/>';
177  $html[] = '</div>';
178  $html[] = '<div id="' . $treeWrapperId . '" class="svg-tree-element" style="height: ' . $heightInPx . 'px;"></div>';
179  $html[] = '</div>';
180  if (!$readOnly && !empty($fieldWizardHtml)) {
181  $html[] = '<div class="form-wizards-items-bottom">';
182  $html[] = $fieldWizardHtml;
183  $html[] = '</div>';
184  }
185  $html[] = '</div>';
186  $html[] = '</div>';
187  $html[] = '</div>';
188 
189  $resultArray['html'] = implode(LF, $html);
190 
191  // add necessary labels for tree header
192  if ($showHeader) {
193  $resultArray['additionalInlineLanguageLabelFiles'][] = 'EXT:core/Resources/Private/Language/locallang_csh_corebe.xlf';
194  }
195  $fieldChangeFuncs = $this->‪getFieldChangeFuncs();
196  if ($this->validateOnFieldChange($fieldChangeFuncs)) {
197  $onFieldChangeItems = $this->getOnFieldChangeItems($fieldChangeFuncs);
198  $resultArray['requireJsModules']['selectTreeElement'] = ‪JavaScriptModuleInstruction::forRequireJS(
199  'TYPO3/CMS/Backend/FormEngine/Element/SelectTreeElement',
200  'SelectTreeElement'
201  )->instance($treeWrapperId, $fieldId, null, $onFieldChangeItems);
202  } else {
203  // @deprecated
204  $resultArray['requireJsModules']['selectTreeElement'] = ['TYPO3/CMS/Backend/FormEngine/Element/SelectTreeElement' => '
205  function(tree) {
206  new tree.SelectTreeElement(' . GeneralUtility::quoteJSvalue($treeWrapperId) . ', ' . GeneralUtility::quoteJSvalue($fieldId) . ', ' . implode(';', $fieldChangeFuncs) . ');
207  }',
208  ];
209  }
210 
211  return $resultArray;
212  }
213 
217  protected function ‪getFieldChangeFuncs(): array
218  {
219  $items = [];
220  $parameterArray = $this->data['parameterArray'];
221  if (!empty($parameterArray['fieldChangeFunc']['TBE_EDITOR_fieldChanged'])) {
222  $items[] = $parameterArray['fieldChangeFunc']['TBE_EDITOR_fieldChanged'];
223  }
224  if (!empty($parameterArray['fieldChangeFunc']['alert'])) {
225  $items[] = $parameterArray['fieldChangeFunc']['alert'];
226  }
227  return $items;
228  }
229 }
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:76
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:120
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪array initializeResultArray()
Definition: AbstractNode.php:91
‪TYPO3\CMS\Backend\Form\Element\SelectTreeElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: SelectTreeElement.php:35
‪TYPO3\CMS\Backend\Form\Element\SelectTreeElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: SelectTreeElement.php:43
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:35
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction
Definition: JavaScriptModuleInstruction.php:23
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:16
‪TYPO3\CMS\Backend\Form\Element\SelectTreeElement
Definition: SelectTreeElement.php:29
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\forRequireJS
‪static self forRequireJS(string $name, string $exportName=null)
Definition: JavaScriptModuleInstruction.php:49
‪TYPO3\CMS\Backend\Form\Element\SelectTreeElement\$minItemsToShow
‪int $minItemsToShow
Definition: SelectTreeElement.php:61
‪TYPO3\CMS\Backend\Form\AbstractNode\getValidationDataAsJsonString
‪string getValidationDataAsJsonString(array $config)
Definition: AbstractNode.php:156
‪TYPO3\CMS\Backend\Form\Element\SelectTreeElement\render
‪array render()
Definition: SelectTreeElement.php:75
‪TYPO3\CMS\Backend\Form\Element\SelectTreeElement\$itemsToShow
‪int $itemsToShow
Definition: SelectTreeElement.php:54
‪TYPO3\CMS\Backend\Form\Element\SelectTreeElement\getFieldChangeFuncs
‪string[] OnFieldChangeInterface[] getFieldChangeFuncs()
Definition: SelectTreeElement.php:211
‪TYPO3\CMS\Backend\Form\Element\SelectTreeElement\$itemHeight
‪int $itemHeight
Definition: SelectTreeElement.php:67
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldWizard
‪array renderFieldWizard()
Definition: AbstractFormElement.php:108