[BUGFIX] FormEngine creating section container in new record
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / FormFlexAjaxController.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Backend\Controller;
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
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
20 use TYPO3\CMS\Backend\Form\FormDataCompiler;
21 use TYPO3\CMS\Backend\Form\FormDataGroup\TcaDatabaseRecord;
22 use TYPO3\CMS\Backend\Form\NodeFactory;
23 use TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools;
24 use TYPO3\CMS\Core\Utility\ArrayUtility;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26 use TYPO3\CMS\Core\Utility\StringUtility;
27
28 /**
29 * Handle FormEngine flex field ajax calls
30 */
31 class FormFlexAjaxController extends AbstractFormEngineAjaxController
32 {
33 /**
34 * Render a single flex form section container to add it to the DOM
35 *
36 * @param ServerRequestInterface $request
37 * @param ResponseInterface $response
38 * @return ResponseInterface
39 */
40 public function containerAdd(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
41 {
42 $queryParameters = $request->getParsedBody();
43
44 $vanillaUid = (int)$queryParameters['vanillaUid'];
45 $databaseRowUid = $queryParameters['databaseRowUid'];
46 $command = $queryParameters['command'];
47 $tableName = $queryParameters['tableName'];
48 $fieldName = $queryParameters['fieldName'];
49 $recordTypeValue = $queryParameters['recordTypeValue'];
50 $dataStructureIdentifier = json_encode($queryParameters['dataStructureIdentifier']);
51 $flexFormSheetName = $queryParameters['flexFormSheetName'];
52 $flexFormFieldName = $queryParameters['flexFormFieldName'];
53 $flexFormContainerName = $queryParameters['flexFormContainerName'];
54
55 // Prepare TCA and data values for a new section container using data providers
56 $processedTca = $GLOBALS['TCA'][$tableName];
57 $flexFormTools = GeneralUtility::makeInstance(FlexFormTools::class);
58 $dataStructure = $flexFormTools->parseDataStructureByIdentifier($dataStructureIdentifier);
59 $processedTca['columns'][$fieldName]['config']['ds'] = $dataStructure;
60 $processedTca['columns'][$fieldName]['config']['dataStructureIdentifier'] = $dataStructureIdentifier;
61 // Get a new unique id for this container.
62 $flexFormContainerIdentifier = StringUtility::getUniqueId();
63 $flexSectionContainerPreparation = [
64 'flexFormSheetName' => $flexFormSheetName,
65 'flexFormFieldName' => $flexFormFieldName,
66 'flexFormContainerName' => $flexFormContainerName,
67 'flexFormContainerIdentifier' => $flexFormContainerIdentifier,
68 ];
69
70 $formDataGroup = GeneralUtility::makeInstance(TcaDatabaseRecord::class);
71 $formDataCompiler = GeneralUtility::makeInstance(FormDataCompiler::class, $formDataGroup);
72 $formDataCompilerInput = [
73 'tableName' => $tableName,
74 'vanillaUid' => (int)$vanillaUid,
75 'command' => $command,
76 'recordTypeValue' => $recordTypeValue,
77 'processedTca' => $processedTca,
78 'flexSectionContainerPreparation' => $flexSectionContainerPreparation,
79 ];
80 // A new container on a new record needs the 'NEW123' uid here, see comment
81 // in DatabaseUniqueUidNewRow for more information on that.
82 // @todo: Resolve, maybe with a redifinition of vanillaUid to transport the information more clean through this var?
83 // @see issue #80100 for a series of changes in this area
84 if ($command === 'new') {
85 $formDataCompilerInput['databaseRow']['uid'] = $databaseRowUid;
86 }
87 $formData = $formDataCompiler->compile($formDataCompilerInput);
88
89 $dataStructure = $formData['processedTca']['columns'][$fieldName]['config']['ds'];
90 $formData['fieldName'] = $fieldName;
91 $formData['flexFormDataStructureArray'] = $dataStructure['sheets'][$flexFormSheetName]['ROOT']['el'][$flexFormFieldName]['children'][$flexFormContainerIdentifier];
92 $formData['flexFormDataStructureIdentifier'] = $dataStructureIdentifier;
93 $formData['flexFormFieldName'] = $flexFormFieldName;
94 $formData['flexFormSheetName'] = $flexFormSheetName;
95 $formData['flexFormContainerName'] = $flexFormContainerName;
96 $formData['flexFormContainerIdentifier'] = $flexFormContainerIdentifier;
97 $formData['flexFormContainerElementCollapsed'] = false;
98
99 $formData['flexFormFormPrefix'] = '[data][' . $flexFormSheetName . '][lDEF]' . '[' . $flexFormFieldName . ']' . '[el]';
100
101 // Set initialized data of that section container from compiler to the array part used
102 // by flexFormElementContainer which prepares parameterArray. Important for initialized
103 // values of group element.
104 if (isset($formData['databaseRow'][$fieldName]
105 ['data'][$flexFormSheetName]
106 ['lDEF'][$flexFormFieldName]
107 ['el'][$flexFormContainerIdentifier][$flexFormContainerName]['el']
108 )
109 && is_array($formData['databaseRow'][$fieldName]
110 ['data'][$flexFormSheetName]
111 ['lDEF'][$flexFormFieldName]
112 ['el'][$flexFormContainerIdentifier][$flexFormContainerName]['el']
113 )
114 ) {
115 $formData['flexFormRowData'] = $formData['databaseRow'][$fieldName]
116 ['data'][$flexFormSheetName]
117 ['lDEF'][$flexFormFieldName]
118 ['el'][$flexFormContainerIdentifier][$flexFormContainerName]['el'];
119 }
120
121 $formData['parameterArray']['itemFormElName'] = 'data[' . $tableName . '][' . $formData['databaseRow']['uid'] . '][' . $fieldName . ']';
122
123 // JavaScript code for event handlers:
124 // @todo: see if we can get rid of this - used in group elements, and also for the "reload" on type field changes
125 $formData['parameterArray']['fieldChangeFunc'] = [];
126 $formData['parameterArray']['fieldChangeFunc']['TBE_EDITOR_fieldChanged'] = 'TBE_EDITOR.fieldChanged('
127 . GeneralUtility::quoteJSvalue($tableName)
128 . ',' . GeneralUtility::quoteJSvalue($formData['databaseRow']['uid'])
129 . ',' . GeneralUtility::quoteJSvalue($fieldName)
130 . ',' . GeneralUtility::quoteJSvalue($formData['parameterArray']['itemFormElName'])
131 . ');';
132
133 // @todo: check GroupElement for usage of elementBaseName ... maybe kick that thing?
134
135 // Feed resulting form data to container structure to render HTML and other result data
136 $nodeFactory = GeneralUtility::makeInstance(NodeFactory::class);
137 $formData['renderType'] = 'flexFormContainerContainer';
138 $newContainerResult = $nodeFactory->create($formData)->render();
139
140 $jsonResult = [
141 'html' => $newContainerResult['html'],
142 'stylesheetFiles' => [],
143 'scriptCall' => [],
144 ];
145
146 if (!empty($newContainerResult['additionalJavaScriptSubmit'])) {
147 $additionalJavaScriptSubmit = implode('', $newContainerResult['additionalJavaScriptSubmit']);
148 $additionalJavaScriptSubmit = str_replace([CR, LF], '', $additionalJavaScriptSubmit);
149 $jsonResult['scriptCall'][] = 'TBE_EDITOR.addActionChecks("submit", "' . addslashes($additionalJavaScriptSubmit) . '");';
150 }
151 foreach ($newContainerResult['additionalJavaScriptPost'] as $singleAdditionalJavaScriptPost) {
152 $jsonResult['scriptCall'][] = $singleAdditionalJavaScriptPost;
153 }
154 foreach ($newContainerResult['stylesheetFiles'] as $stylesheetFile) {
155 $jsonResult['stylesheetFiles'][] = $this->getRelativePathToStylesheetFile($stylesheetFile);
156 }
157 if (!empty($newContainerResult['additionalInlineLanguageLabelFiles'])) {
158 $labels = [];
159 foreach ($newContainerResult['additionalInlineLanguageLabelFiles'] as $additionalInlineLanguageLabelFile) {
160 ArrayUtility::mergeRecursiveWithOverrule(
161 $labels,
162 $this->getLabelsFromLocalizationFile($additionalInlineLanguageLabelFile)
163 );
164 }
165 $javaScriptCode = [];
166 $javaScriptCode[] = 'if (typeof TYPO3 === \'undefined\' || typeof TYPO3.lang === \'undefined\') {';
167 $javaScriptCode[] = ' TYPO3.lang = {}';
168 $javaScriptCode[] = '}';
169 $javaScriptCode[] = 'var additionalInlineLanguageLabels = ' . json_encode($labels) . ';';
170 $javaScriptCode[] = 'for (var attributeName in additionalInlineLanguageLabels) {';
171 $javaScriptCode[] = ' if (typeof TYPO3.lang[attributeName] === \'undefined\') {';
172 $javaScriptCode[] = ' TYPO3.lang[attributeName] = additionalInlineLanguageLabels[attributeName]';
173 $javaScriptCode[] = ' }';
174 $javaScriptCode[] = '}';
175
176 $jsonResult['scriptCall'][] = implode(LF, $javaScriptCode);
177 }
178
179 $requireJsModule = $this->createExecutableStringRepresentationOfRegisteredRequireJsModules($newContainerResult);
180 $jsonResult['scriptCall'] = array_merge($requireJsModule, $jsonResult['scriptCall']);
181
182 $response->getBody()->write(json_encode($jsonResult));
183
184 return $response;
185 }
186 }