‪TYPO3CMS  ‪main
FormSelectTreeAjaxController.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 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
30 
34 #[AsController]
36 {
37  public function ‪__construct(
38  private readonly ‪FormDataCompiler $formDataCompiler,
39  private readonly ‪FlexFormTools $flexFormTools,
40  ) {}
41 
47  public function ‪fetchDataAction(ServerRequestInterface $request): ResponseInterface
48  {
49  $tableName = $request->getQueryParams()['tableName'] ?? '';
50  $fieldName = $request->getQueryParams()['fieldName'] ?? '';
51 
52  // Prepare processedTca: Remove all column definitions except the one that contains
53  // our tree definition. This way only this field is calculated, everything else is ignored.
54  if (!isset(‪$GLOBALS['TCA'][$tableName]) || !is_array(‪$GLOBALS['TCA'][$tableName])) {
55  throw new \RuntimeException(
56  'TCA for table ' . $tableName . ' not found',
57  1479386729
58  );
59  }
60  $processedTca = ‪$GLOBALS['TCA'][$tableName];
61  if (!isset($processedTca['columns'][$fieldName]) || !is_array($processedTca['columns'][$fieldName])) {
62  throw new \RuntimeException(
63  'TCA for table ' . $tableName . ' and field ' . $fieldName . ' not found',
64  1479386990
65  );
66  }
67 
68  // Force given record type and set showitem to our field only
69  $recordTypeValue = $request->getQueryParams()['recordTypeValue'];
70  $processedTca['types'][$recordTypeValue]['showitem'] = $fieldName;
71  // Unset all columns except our field
72  $processedTca['columns'] = [
73  $fieldName => $processedTca['columns'][$fieldName],
74  ];
75 
76  $dataStructureIdentifier = '';
77  $flexFormSheetName = '';
78  $flexFormFieldName = '';
79  $flexFormContainerIdentifier = '';
80  $flexFormContainerFieldName = '';
81  $flexSectionContainerPreparation = [];
82  if ($processedTca['columns'][$fieldName]['config']['type'] === 'flex') {
83  if (!empty($request->getQueryParams()['dataStructureIdentifier'])) {
84  $dataStructureIdentifier = $request->getQueryParams()['dataStructureIdentifier'];
85  }
86  $flexFormSheetName = $request->getQueryParams()['flexFormSheetName'];
87  $flexFormFieldName = $request->getQueryParams()['flexFormFieldName'];
88  $flexFormContainerName = $request->getQueryParams()['flexFormContainerName'];
89  $flexFormContainerIdentifier = $request->getQueryParams()['flexFormContainerIdentifier'];
90  $flexFormContainerFieldName = $request->getQueryParams()['flexFormContainerFieldName'];
91  $flexFormSectionContainerIsNew = (bool)$request->getQueryParams()['flexFormSectionContainerIsNew'];
92 
93  $dataStructure = $this->flexFormTools->parseDataStructureByIdentifier($dataStructureIdentifier);
94 
95  // Reduce given data structure down to the relevant element only
96  if (empty($flexFormContainerFieldName)) {
97  if (isset($dataStructure['sheets'][$flexFormSheetName]['ROOT']
98  ['el'][$flexFormFieldName])
99  ) {
100  $dataStructure = [
101  'sheets' => [
102  $flexFormSheetName => [
103  'ROOT' => [
104  'type' => 'array',
105  'el' => [
106  $flexFormFieldName => $dataStructure['sheets'][$flexFormSheetName]['ROOT']
107  ['el'][$flexFormFieldName],
108  ],
109  ],
110  ],
111  ],
112  ];
113  }
114  } elseif (isset($dataStructure['sheets'][$flexFormSheetName]['ROOT']
115  ['el'][$flexFormFieldName]
116  ['el'][$flexFormContainerName]
117  ['el'][$flexFormContainerFieldName])
118  ) {
119  // If this is a tree in a section container that has just been added by the FlexFormAjaxController
120  // "new container" action, then this container is not yet persisted, so we need to trigger the
121  // TcaFlexProcess data provider again to prepare the DS and databaseRow of that container.
122  if ($flexFormSectionContainerIsNew) {
123  $flexSectionContainerPreparation = [
124  'flexFormSheetName' => $flexFormSheetName,
125  'flexFormFieldName' => $flexFormFieldName,
126  'flexFormContainerName' => $flexFormContainerName,
127  'flexFormContainerIdentifier' => $flexFormContainerIdentifier,
128  ];
129  }
130  // Now restrict the data structure to our tree element only
131  $dataStructure = [
132  'sheets' => [
133  $flexFormSheetName => [
134  'ROOT' => [
135  'type' => 'array',
136  'el' => [
137  $flexFormFieldName => [
138  'section' => 1,
139  'type' => 'array',
140  'el' => [
141  $flexFormContainerName => [
142  'type' => 'array',
143  'el' => [
144  $flexFormContainerFieldName => $dataStructure['sheets'][$flexFormSheetName]['ROOT']
145  ['el'][$flexFormFieldName]
146  ['el'][$flexFormContainerName]
147  ['el'][$flexFormContainerFieldName],
148  ],
149  ],
150  ],
151  ],
152  ],
153  ],
154  ],
155  ],
156  ];
157  }
158  $processedTca['columns'][$fieldName]['config']['ds'] = $dataStructure;
159  $processedTca['columns'][$fieldName]['config']['dataStructureIdentifier'] = $dataStructureIdentifier;
160  }
161 
162  $formDataCompilerInput = [
163  'request' => $request,
164  'tableName' => $tableName,
165  'vanillaUid' => (int)$request->getQueryParams()['uid'],
166  'command' => $request->getQueryParams()['command'],
167  'processedTca' => $processedTca,
168  'recordTypeValue' => $recordTypeValue,
169  'selectTreeCompileItems' => true,
170  'flexSectionContainerPreparation' => $flexSectionContainerPreparation,
171  ];
172  if (!empty($request->getQueryParams()['overrideValues'])) {
173  $formDataCompilerInput['overrideValues'] = json_decode($request->getQueryParams()['overrideValues'], true);
174  }
175  if (!empty($request->getQueryParams()['defaultValues'])) {
176  $formDataCompilerInput['defaultValues'] = json_decode($request->getQueryParams()['defaultValues'], true);
177  }
178  $formData = $this->formDataCompiler->compile($formDataCompilerInput, GeneralUtility::makeInstance(TcaSelectTreeAjaxFieldData::class));
179 
180  if ($formData['processedTca']['columns'][$fieldName]['config']['type'] === 'flex') {
181  if (empty($flexFormContainerFieldName)) {
182  $treeData = $formData['processedTca']['columns'][$fieldName]['config']['ds']
183  ['sheets'][$flexFormSheetName]['ROOT']
184  ['el'][$flexFormFieldName]['config']['items'];
185  } else {
186  $treeData = $formData['processedTca']['columns'][$fieldName]['config']['ds']
187  ['sheets'][$flexFormSheetName]['ROOT']
188  ['el'][$flexFormFieldName]
189  ['children'][$flexFormContainerIdentifier]
190  ['el'][$flexFormContainerFieldName]['config']['items'];
191  }
192  } else {
193  $treeData = $formData['processedTca']['columns'][$fieldName]['config']['items'];
194  }
195 
196  $data = [];
197  foreach ($treeData ?? [] as $item) {
198  $treeItem = new ‪SelectTreeItem(
199  item: new ‪TreeItem(
200  identifier: (string)$item['identifier'],
201  parentIdentifier: (string)($item['parentIdentifier'] ?? ''),
202  recordType: (string)($item['recordType'] ?? ''),
203  name: (string)($item['name'] ?? ''),
204  ‪prefix: (string)($item['prefix'] ?? ''),
205  suffix: (string)($item['suffix'] ?? ''),
206  tooltip: (string)($item['tooltip'] ?? ''),
207  depth: (int)($item['depth'] ?? 0),
208  hasChildren: (bool)($item['hasChildren'] ?? false),
209  expanded: (bool)($item['expanded'] ?? false),
210  loaded: true,
211  icon: (string)($item['icon'] ?? ''),
212  overlayIcon: (string)($item['overlayIcon'] ?? ''),
213  statusInformation: (array)($item['statusInformation'] ?? []),
214  labels: (array)($item['labels'] ?? []),
215  ),
216  checked: (bool)($item['checked'] ?? false),
217  selectable: (bool)($item['selectable'] ?? false),
218  );
219  $data[] = $treeItem;
220  }
221 
222  return new ‪JsonResponse($data);
223  }
224 }
‪TYPO3\CMS\Backend\Controller\FormSelectTreeAjaxController
Definition: FormSelectTreeAjaxController.php:36
‪TYPO3\CMS\Backend\Dto\Tree\SelectTreeItem
Definition: SelectTreeItem.php:24
‪TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools
Definition: FlexFormTools.php:40
‪TYPO3\CMS\Backend\Controller\FormSelectTreeAjaxController\fetchDataAction
‪fetchDataAction(ServerRequestInterface $request)
Definition: FormSelectTreeAjaxController.php:47
‪TYPO3\CMS\Backend\Form\FormDataGroup\TcaSelectTreeAjaxFieldData
Definition: TcaSelectTreeAjaxFieldData.php:26
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:28
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Attribute\AsController
Definition: AsController.php:25
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Form\FormDataCompiler
Definition: FormDataCompiler.php:26
‪TYPO3\CMS\Backend\Dto\Tree\TreeItem
Definition: TreeItem.php:27
‪TYPO3\CMS\Extbase\Security\prefix
‪@ prefix
Definition: HashScope.php:30
‪TYPO3\CMS\Backend\Controller
Definition: AboutController.php:18
‪TYPO3\CMS\Backend\Controller\FormSelectTreeAjaxController\__construct
‪__construct(private readonly FormDataCompiler $formDataCompiler, private readonly FlexFormTools $flexFormTools,)
Definition: FormSelectTreeAjaxController.php:37