‪TYPO3CMS  10.4
FormSelectTreeAjaxController.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 Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
25 
30 {
38  public function ‪fetchDataAction(ServerRequestInterface $request): ResponseInterface
39  {
40  $tableName = $request->getQueryParams()['tableName'] ?? '';
41  $fieldName = $request->getQueryParams()['fieldName'] ?? '';
42 
43  // Prepare processedTca: Remove all column definitions except the one that contains
44  // our tree definition. This way only this field is calculated, everything else is ignored.
45  if (!isset(‪$GLOBALS['TCA'][$tableName]) || !is_array(‪$GLOBALS['TCA'][$tableName])) {
46  throw new \RuntimeException(
47  'TCA for table ' . $tableName . ' not found',
48  1479386729
49  );
50  }
51  $processedTca = ‪$GLOBALS['TCA'][$tableName];
52  if (!isset($processedTca['columns'][$fieldName]) || !is_array($processedTca['columns'][$fieldName])) {
53  throw new \RuntimeException(
54  'TCA for table ' . $tableName . ' and field ' . $fieldName . ' not found',
55  1479386990
56  );
57  }
58 
59  // Force given record type and set showitem to our field only
60  $recordTypeValue = $request->getQueryParams()['recordTypeValue'];
61  $processedTca['types'][$recordTypeValue]['showitem'] = $fieldName;
62  // Unset all columns except our field
63  $processedTca['columns'] = [
64  $fieldName => $processedTca['columns'][$fieldName],
65  ];
66 
67  $dataStructureIdentifier = '';
68  $flexFormSheetName = '';
69  $flexFormFieldName = '';
70  $flexFormContainerIdentifier = '';
71  $flexFormContainerFieldName = '';
72  $flexSectionContainerPreparation = [];
73  if ($processedTca['columns'][$fieldName]['config']['type'] === 'flex') {
74  if (!empty($request->getQueryParams()['dataStructureIdentifier'])) {
75  $dataStructureIdentifier = json_encode($request->getQueryParams()['dataStructureIdentifier']);
76  }
77  $flexFormSheetName = $request->getQueryParams()['flexFormSheetName'];
78  $flexFormFieldName = $request->getQueryParams()['flexFormFieldName'];
79  $flexFormContainerName = $request->getQueryParams()['flexFormContainerName'];
80  $flexFormContainerIdentifier = $request->getQueryParams()['flexFormContainerIdentifier'];
81  $flexFormContainerFieldName = $request->getQueryParams()['flexFormContainerFieldName'];
82  $flexFormSectionContainerIsNew = (bool)$request->getQueryParams()['flexFormSectionContainerIsNew'];
83 
84  $flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
85  $dataStructure = $flexFormTools->parseDataStructureByIdentifier($dataStructureIdentifier);
86 
87  // Reduce given data structure down to the relevant element only
88  if (empty($flexFormContainerFieldName)) {
89  if (isset($dataStructure['sheets'][$flexFormSheetName]['ROOT']
90  ['el'][$flexFormFieldName])
91  ) {
92  $dataStructure = [
93  'sheets' => [
94  $flexFormSheetName => [
95  'ROOT' => [
96  'type' => 'array',
97  'el' => [
98  $flexFormFieldName => $dataStructure['sheets'][$flexFormSheetName]['ROOT']
99  ['el'][$flexFormFieldName],
100  ],
101  ],
102  ],
103  ],
104  ];
105  }
106  } elseif (isset($dataStructure['sheets'][$flexFormSheetName]['ROOT']
107  ['el'][$flexFormFieldName]
108  ['el'][$flexFormContainerName]
109  ['el'][$flexFormContainerFieldName])
110  ) {
111  // If this is a tree in a section container that has just been added by the FlexFormAjaxController
112  // "new container" action, then this container is not yet persisted, so we need to trigger the
113  // TcaFlexProcess data provider again to prepare the DS and databaseRow of that container.
114  if ($flexFormSectionContainerIsNew) {
115  $flexSectionContainerPreparation = [
116  'flexFormSheetName' => $flexFormSheetName,
117  'flexFormFieldName' => $flexFormFieldName,
118  'flexFormContainerName' => $flexFormContainerName,
119  'flexFormContainerIdentifier' => $flexFormContainerIdentifier,
120  ];
121  }
122  // Now restrict the data structure to our tree element only
123  $dataStructure = [
124  'sheets' => [
125  $flexFormSheetName => [
126  'ROOT' => [
127  'type' => 'array',
128  'el' => [
129  $flexFormFieldName => [
130  'section' => 1,
131  'type' => 'array',
132  'el' => [
133  $flexFormContainerName => [
134  'type' => 'array',
135  'el' => [
136  $flexFormContainerFieldName => $dataStructure['sheets'][$flexFormSheetName]['ROOT']
137  ['el'][$flexFormFieldName]
138  ['el'][$flexFormContainerName]
139  ['el'][$flexFormContainerFieldName]
140  ],
141  ],
142  ],
143  ],
144  ],
145  ],
146  ],
147  ],
148  ];
149  }
150  $processedTca['columns'][$fieldName]['config']['ds'] = $dataStructure;
151  $processedTca['columns'][$fieldName]['config']['dataStructureIdentifier'] = $dataStructureIdentifier;
152  }
153 
154  $formDataGroup = GeneralUtility::makeInstance(TcaSelectTreeAjaxFieldData::class);
155  $formDataCompiler = GeneralUtility::makeInstance(FormDataCompiler::class, $formDataGroup);
156  $formDataCompilerInput = [
157  'tableName' => $tableName,
158  'vanillaUid' => (int)$request->getQueryParams()['uid'],
159  'command' => $request->getQueryParams()['command'],
160  'processedTca' => $processedTca,
161  'recordTypeValue' => $recordTypeValue,
162  'selectTreeCompileItems' => true,
163  'flexSectionContainerPreparation' => $flexSectionContainerPreparation,
164  ];
165  $formData = $formDataCompiler->compile($formDataCompilerInput);
166 
167  if ($formData['processedTca']['columns'][$fieldName]['config']['type'] === 'flex') {
168  if (empty($flexFormContainerFieldName)) {
169  $treeData = $formData['processedTca']['columns'][$fieldName]['config']['ds']
170  ['sheets'][$flexFormSheetName]['ROOT']
171  ['el'][$flexFormFieldName]['config']['items'];
172  } else {
173  $treeData = $formData['processedTca']['columns'][$fieldName]['config']['ds']
174  ['sheets'][$flexFormSheetName]['ROOT']
175  ['el'][$flexFormFieldName]
176  ['children'][$flexFormContainerIdentifier]
177  ['el'][$flexFormContainerFieldName]['config']['items'];
178  }
179  } else {
180  $treeData = $formData['processedTca']['columns'][$fieldName]['config']['items'];
181  }
182  return (new ‪JsonResponse())->setPayload($treeData ?? []);
183  }
184 }
‪TYPO3\CMS\Backend\Controller\FormSelectTreeAjaxController
Definition: FormSelectTreeAjaxController.php:30
‪TYPO3\CMS\Backend\Controller\FormSelectTreeAjaxController\fetchDataAction
‪ResponseInterface fetchDataAction(ServerRequestInterface $request)
Definition: FormSelectTreeAjaxController.php:38
‪TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools
Definition: FlexFormTools.php:38
‪TYPO3\CMS\Backend\Form\FormDataGroup\TcaSelectTreeAjaxFieldData
Definition: TcaSelectTreeAjaxFieldData.php:26
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:26
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Backend\Form\FormDataCompiler
Definition: FormDataCompiler.php:25
‪TYPO3\CMS\Backend\Controller
Definition: AbstractFormEngineAjaxController.php:18