‪TYPO3CMS  ‪main
CategoryElement.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
22 
27 {
28  private const ‪MIN_ITEMS_COUNT = 5;
29  private const ‪DEFAULT_ITEMS_COUNT = 15;
30  private const ‪ITEM_HEIGHT_BASE = 20;
31 
35  protected ‪$defaultFieldInformation = [
36  'tcaDescription' => [
37  'renderType' => 'tcaDescription',
38  ],
39  ];
40 
44  protected ‪$defaultFieldWizard = [
45  'localizationStateSelector' => [
46  'renderType' => 'localizationStateSelector',
47  ],
48  ];
49 
53  public function ‪render(): array
54  {
55  $resultArray = $this->‪initializeResultArray();
56  $fieldName = $this->data['fieldName'];
57  $tableName = $this->data['tableName'];
58  $parameterArray = $this->data['parameterArray'];
59  $formElementId = md5($parameterArray['itemFormElName']);
60 
61  // Field configuration from TCA:
62  $config = $parameterArray['fieldConf']['config'];
63  $readOnly = (bool)($config['readOnly'] ?? false);
64  $expanded = (bool)($config['treeConfig']['appearance']['expandAll'] ?? false);
65  $showHeader = (bool)($config['treeConfig']['appearance']['showHeader'] ?? false);
66  $exclusiveKeys = $config['exclusiveKeys'] ?? '';
67  $height = ((int)($config['size'] ?? 0) > 0)
68  ? max(self::MIN_ITEMS_COUNT, (int)$config['size'])
69  : self::DEFAULT_ITEMS_COUNT;
70  $heightInPx = $height * ‪self::ITEM_HEIGHT_BASE;
71  $treeWrapperId = 'tree_' . $formElementId;
72  $fieldId = 'tree_record_' . $formElementId;
73 
74  $dataStructureIdentifier = '';
75  $flexFormSheetName = '';
76  $flexFormFieldName = '';
77  $flexFormContainerName = '';
78  $flexFormContainerIdentifier = '';
79  $flexFormContainerFieldName = '';
80  $flexFormSectionContainerIsNew = false;
81  if ($this->data['processedTca']['columns'][$fieldName]['config']['type'] === 'flex') {
82  $dataStructureIdentifier = $this->data['processedTca']['columns'][$fieldName]['config']['dataStructureIdentifier'];
83  if (isset($this->data['flexFormSheetName'])) {
84  $flexFormSheetName = $this->data['flexFormSheetName'];
85  }
86  if (isset($this->data['flexFormFieldName'])) {
87  $flexFormFieldName = $this->data['flexFormFieldName'];
88  }
89  if (isset($this->data['flexFormContainerName'])) {
90  $flexFormContainerName = $this->data['flexFormContainerName'];
91  }
92  if (isset($this->data['flexFormContainerFieldName'])) {
93  $flexFormContainerFieldName = $this->data['flexFormContainerFieldName'];
94  }
95  if (isset($this->data['flexFormContainerIdentifier'])) {
96  $flexFormContainerIdentifier = $this->data['flexFormContainerIdentifier'];
97  }
98  // Add a flag this is a tree in a new flex section container element. This is needed to initialize
99  // the databaseRow with this container again so the tree data provider is able to calculate tree items.
100  if (!empty($this->data['flexSectionContainerPreparation'])) {
101  $flexFormSectionContainerIsNew = true;
102  }
103  }
104 
105  $fieldInformationResult = $this->‪renderFieldInformation();
106  $fieldInformationHtml = $fieldInformationResult['html'];
107  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
108 
109  $fieldWizardResult = $this->‪renderFieldWizard();
110  $fieldWizardHtml = $fieldWizardResult['html'];
111  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
112 
113  if (!$readOnly && !empty($fieldWizardHtml)) {
114  $fieldWizardHtml = '<div class="form-wizards-items-bottom">' . $fieldWizardHtml . '</div>';
115  }
116 
117  $recordElementAttributes = [
118  'id' => $fieldId,
119  'type' => 'hidden',
120  'class' => 'treeRecord',
121  'name' => $parameterArray['itemFormElName'],
122  'value' => implode(',', $parameterArray['itemFormElValue']),
123  'data-uid' => (int)$this->data['vanillaUid'],
124  'data-command' => $this->data['command'],
125  'data-fieldname' => $fieldName,
126  'data-tablename' => $tableName,
127  'data-read-only' => $readOnly,
128  'data-tree-show-toolbar' => $showHeader,
129  'data-recordtypevalue' => $this->data['recordTypeValue'],
130  'data-relatedfieldname' => $parameterArray['itemFormElName'],
131  'data-flexformsheetname' => $flexFormSheetName,
132  'data-flexformfieldname' => $flexFormFieldName,
133  'data-tree-exclusive-keys' => $exclusiveKeys,
134  'data-flexformcontainername' => $flexFormContainerName,
135  'data-datastructureidentifier' => $dataStructureIdentifier,
136  'data-tree-expand-up-to-level' => $expanded ? '999' : '1',
137  'data-flexformcontainerfieldname' => $flexFormContainerFieldName,
138  'data-flexformcontaineridentifier' => $flexFormContainerIdentifier,
139  'data-formengine-validation-rules' => $this->‪getValidationDataAsJsonString($config),
140  'data-flexformsectioncontainerisnew' => (string)$flexFormSectionContainerIsNew,
141  'data-overridevalues' => GeneralUtility::jsonEncodeForHtmlAttribute($this->data['overrideValues'], false),
142  'data-defaultvalues' => GeneralUtility::jsonEncodeForHtmlAttribute($this->data['defaultValues'], false),
143  ];
144 
145  $resultArray['html'] = $this->‪wrapWithFieldsetAndLegend(
146  '<typo3-formengine-element-category ' . GeneralUtility::implodeAttributes(['class' => 'formengine-field-item t3js-formengine-field-item', 'recordFieldId' => $fieldId, 'treeWrapperId' => $treeWrapperId], true) . '>
147  ' . $fieldInformationHtml . '
148  <div class="form-control-wrap">
149  <div class="form-wizards-wrap">
150  <div class="form-wizards-element">
151  <div class="typo3-tceforms-tree">
152  <input ' . GeneralUtility::implodeAttributes(array_map(strval(...), $recordElementAttributes), true, true) . '/>
153  </div>
154  <div id="' . htmlspecialchars($treeWrapperId) . '" class="tree-element" style="height: ' . $heightInPx . 'px;"></div>
155  </div>
156  ' . $fieldWizardHtml . '
157  </div>
158  </div>
159  </typo3-formengine-element-category>'
160  );
161 
162  $resultArray['javaScriptModules'][] = ‪JavaScriptModuleInstruction::create('@typo3/backend/form-engine/element/category-element.js');
163 
164  return $resultArray;
165  }
166 }
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:73
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:104
‪TYPO3\CMS\Backend\Form\Element\CategoryElement
Definition: CategoryElement.php:27
‪TYPO3\CMS\Backend\Form\Element\CategoryElement\MIN_ITEMS_COUNT
‪const MIN_ITEMS_COUNT
Definition: CategoryElement.php:28
‪TYPO3\CMS\Backend\Form\Element\CategoryElement\ITEM_HEIGHT_BASE
‪const ITEM_HEIGHT_BASE
Definition: CategoryElement.php:30
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction\create
‪static create(string $name, string $exportName=null)
Definition: JavaScriptModuleInstruction.php:47
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:37
‪TYPO3\CMS\Core\Page\JavaScriptModuleInstruction
Definition: JavaScriptModuleInstruction.php:23
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:16
‪TYPO3\CMS\Backend\Form\Element\CategoryElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: CategoryElement.php:42
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\wrapWithFieldsetAndLegend
‪wrapWithFieldsetAndLegend(string $innerHTML)
Definition: AbstractFormElement.php:133
‪TYPO3\CMS\Backend\Form\Element\CategoryElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: CategoryElement.php:34
‪TYPO3\CMS\Backend\Form\Element\CategoryElement\render
‪render()
Definition: CategoryElement.php:51
‪TYPO3\CMS\Backend\Form\AbstractNode\getValidationDataAsJsonString
‪getValidationDataAsJsonString(array $config)
Definition: AbstractNode.php:133
‪TYPO3\CMS\Backend\Form\Element\CategoryElement\DEFAULT_ITEMS_COUNT
‪const DEFAULT_ITEMS_COUNT
Definition: CategoryElement.php:29
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldWizard
‪array renderFieldWizard()
Definition: AbstractFormElement.php:105
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪initializeResultArray()
Definition: AbstractNode.php:77