‪TYPO3CMS  ‪main
FormDataCompiler.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\ServerRequestInterface;
20 
26 {
35  public function ‪compile(array $initialData, ‪FormDataGroupInterface $formDataGroup): array
36  {
37  $result = $this->‪initializeResultArray();
38 
39  // There must be only keys that actually exist in result data.
40  $keysNotInResult = array_diff(array_keys($initialData), array_keys($result));
41  if (!empty($keysNotInResult)) {
42  throw new \InvalidArgumentException(
43  'Array keys ' . implode(',', $keysNotInResult) . ' do not exist in result array and can not be set',
44  1440601540
45  );
46  }
47 
48  foreach ($initialData as $dataKey => $dataValue) {
49  if ($dataKey === 'command') {
50  // Sanitize $command
51  if ($dataValue !== 'edit' && $dataValue !== 'new') {
52  throw new \InvalidArgumentException('Command must be either "edit" or "new"', 1437653136);
53  }
54  }
55  if ($dataKey === 'tableName') {
56  // Sanitize $tableName
57  if (empty($dataValue)) {
58  throw new \InvalidArgumentException('No $tableName given', 1437654409);
59  }
60  }
61  if ($dataKey === 'vanillaUid') {
62  if (!‪MathUtility::canBeInterpretedAsInteger($dataValue) && !str_starts_with($dataValue, 'NEW')) {
63  throw new \InvalidArgumentException('$vanillaUid is not an integer or "NEW..." string ID', 1437654247);
64  }
65  if (isset($initialData['command']) && $initialData['command'] === 'edit' && $dataValue < 0) {
66  throw new \InvalidArgumentException('Negative $vanillaUid is not supported with $command="edit', 1437654332);
67  }
68  }
69  $result[$dataKey] = $dataValue;
70  }
71 
72  if (!$result['request'] instanceof ServerRequestInterface) {
73  throw new \RuntimeException(
74  'The current ServerRequestInterface must be provided in key "request"',
75  1686867720
76  );
77  }
78 
79  // Call the data group provider but take care it does not add or remove result keys
80  // This is basically a safety measure against data providers colliding with our array "contract"
81  $resultKeysBeforeFormDataGroup = array_keys($result);
82 
83  $result = $formDataGroup->‪compile($result);
84 
85  if (!is_array($result)) {
86  throw new \UnexpectedValueException(
87  'Data group provider must return array',
88  1446664764
89  );
90  }
91 
92  if (!empty($result['renderData'])) {
93  throw new \RuntimeException(
94  'Array \'renderData\' not empty. Data providers must not add data here',
95  1485201279
96  );
97  }
98 
99  $resultKeysAfterFormDataGroup = array_keys($result);
100 
101  if ($resultKeysAfterFormDataGroup !== $resultKeysBeforeFormDataGroup) {
102  throw new \UnexpectedValueException(
103  'Data group provider must not change result key list',
104  1438079402
105  );
106  }
107 
108  return $result;
109  }
110 
111  private function ‪initializeResultArray(): array
112  {
113  return [
114  // Current ServerRequestInterface. Controllers using FormDataCompiler must provide the current
115  // request object, various data providers and nodes rely on this.
116  'request' => null,
117  // Either "edit" or "new"
118  'command' => '',
119  // Table name of the handled row
120  'tableName' => '',
121  // Not changed uid of the record, meaning of value depends on context (new / edit)
122  // * If $command is "edit"
123  // ** $vanillaUid is a positive integer > 0 pointing to the record in the table
124  // * If $command is "new":
125  // ** If $vanillaUid > 0, it is the uid of a page the record should be added at
126  // ** If $vanillaUid < 0, it is the uid of a record in the same table after which the new record should be added
127  // ** If $vanillaUid = 0, a new record is added on page 0
128  // ** If $vanillaUid = "NEW..." Id of a parent page record if an inline child is added to a not yet persisted page
129  'vanillaUid' => 0,
130  // Url to return to
131  'returnUrl' => null,
132  // Title of the handled record.
133  'recordTitle' => '',
134  // Parent page record is either the full row of the parent page the record is located at or should
135  // be added to, or it is NULL, if a record is added or edited below the root page node.
136  'parentPageRow' => null,
137  // If a translated page is handled, the page row of the default language (the page against all page checks
138  // are made) is set here
139  'defaultLanguagePageRow' => null,
140  // Holds the "neighbor" row if incoming vanillaUid is negative and record creation is relative to a row of the same table.
141  'neighborRow' => null,
142  // For "new" this is the fully initialized row with defaults
143  // The database row. For "edit" workspaceOL() was applied already.
144  // @todo: rename to valueStructure or handledData or similar
145  'databaseRow' => [],
146  // The "effective" page uid we're working on. This is the uid of a page if a page is edited, or the uid
147  // of the parent page if a page or other record is added, or 0 if a record is added or edited below root node.
148  'effectivePid' => 0,
149  // Rootline of page the record that is handled is located at as created by BackendUtility::BEgetRootline()
150  'rootline' => [],
151  // The resolved SiteInterface object of the page or page given record is located on.
152  'site' => null,
153  // For "edit", this is the permission bitmask of the page that is edited, or of the page a record is located at
154  // For "new", this is the permission bitmask of the page the record is added to
155  // @todo: Remove if not needed on a lower level
156  'userPermissionOnPage' => 0,
157  // Full user TSconfig
158  'userTsConfig' => [],
159  // Full page TSconfig of the page that is edited or of the parent page if a record is added.
160  // After type of handled record has been determined, record type specific settings from
161  // [TCEFORM.][tableName.][field.][types.][type.] are merged into [TCEFORM.][tableName.][field.].
162  // Array keys still contain the concatenation dots.
163  'pageTsConfig' => [],
164  // List of available system languages. Array key is the system language uid, value array
165  // contains details of the record, with iso code resolved. Key is the sys_language_uid uid.
166  'systemLanguageRows' => [],
167  // If the page that is handled has "page_language_overlay" records (page has localizations in
168  // different languages), then this array holds those rows.
169  'pageLanguageOverlayRows' => [],
170  // If the handled row is a localized row, this entry hold the default language row array
171  'defaultLanguageRow' => null,
172  // If the handled row is a localized row and $TCA[<tableName>]['ctrl']['translationSource'] is configured,
173  // This entry holds the row of the language source record.
174  'sourceLanguageRow' => null,
175  // If the handled row is a localized row and a transOrigDiffSourceField is defined, this
176  // is the json_decode() version of it. The diff source field is basically a shadow version
177  // of the default language record at the time when the language overlay record was created.
178  // This is used later to compare the default record with this content to show a "diff" if
179  // the default language record changed meanwhile.
180  'defaultLanguageDiffRow' => null,
181  // With userTS options.additionalPreviewLanguages set, field values of additional languages
182  // can be shown. This array holds those additional language records, Array key is sys_language_uid.
183  'additionalLanguageRows' => [],
184  // The tca record type value of the record. Forced to string, there can be "named" type values.
185  'recordTypeValue' => '',
186  // TCA of table with processed fields. After processing, this array contains merged and resolved
187  // array data, items were resolved, only used types are set, renderTypes are set.
188  'processedTca' => [],
189  // List of columns to be processed by data provider. Array value is the column name.
190  'columnsToProcess' => [],
191  // If set to TRUE, no wizards are calculated and rendered later
192  'disabledWizards' => false,
193 
194  // Flex form field data handling is done in a separated FormDataCompiler instance. The full databaseRow
195  // of the record this flex form is embedded in is transferred in case features like single fields
196  // itemsProcFunc need to have this data at hand to do their job.
197  'flexParentDatabaseRow' => [],
198  // If not empty, it tells the TcaFlexProcess data provider to not calculate existing flex fields and
199  // existing flex container sections, but to instead prepare field values and the data structure TCA
200  // for a new container section. This is used by FormFlexAjaxController, the array contains details
201  // which container of which flex field should be created.
202  'flexSectionContainerPreparation' => [],
203 
204  // If true, TcaSelectTreeItems data provider will compile tree items. This is false by default since
205  // on opening a record items are not calculated but are fetch in an ajax request, see FormSelectTreeAjaxController.
206  'selectTreeCompileItems' => false,
207 
208  // BackendUser->uc['inlineView'] - This array holds status of expand / collapsed inline items
209  // This array is "flat", an inline structure with parent uid 1 having firstChild uid 2 having secondChild uid 3
210  // firstChild and secondChild are not nested. If a uid is set it means "record is expanded", example:
211  // 'parent' => [
212  // 1 => [
213  // 'firstChild' => [ 2 ], // record 2 of firstChild table is open in inline context to parent 1
214  // 'secondChild' => [ 3 ], // record 3 of secondChild table is open in inline context to parent 1
215  // ],
216  // ]
217  'inlineExpandCollapseStateArray' => [],
218  // The "entry" pid for inline records. Nested inline records can potentially hang around on different
219  // pid's, but the entry pid is needed for AJAX calls, so that they would know where the action takes
220  // place on the page structure. This is usually an int, but can be a "NEW..." string if an inline relation
221  // is added to a currently being created page.
222  'inlineFirstPid' => null,
223  // The "config" section of an inline parent, prepared and sanitized by TcaInlineConfiguration provider
224  'inlineParentConfig' => [],
225  // Flag that is enabled if a records is child of an inline parent
226  'isInlineChild' => false,
227  // Flag if an inline child is expanded so that additional fields need to be rendered
228  'isInlineChildExpanded' => false,
229  // Flag if the inline is in an ajax context that wants to expand the element
230  'isInlineAjaxOpeningContext' => false,
231  // Uid of the direct parent of the inline element. Handled as string since it may be a "NEW123" string
232  'inlineParentUid' => '',
233  // Table name of the direct parent of the inline element
234  'inlineParentTableName' => '',
235  // Field name of the direct parent of the inline element
236  'inlineParentFieldName' => '',
237  // Uid of the top most parent element. Handled as string since it may be a "NEW123" string
238  'inlineTopMostParentUid' => '',
239  // Table name of the top most parent element
240  'inlineTopMostParentTableName' => '',
241  // Field name of the top most parent element
242  'inlineTopMostParentFieldName' => '',
243 
244  // If is on symmetric side of an inline child parent reference.
245  // symmetric side can be achieved in case of an mm relation to the same table. If record A has a relation
246  // to record B, the symmetric side is set in case that record B gets edited.
247  // Record A (table1) <=> mm <=> Record B (table1)
248  'isOnSymmetricSide' => false,
249 
250  // Uid of a "child-child" if a new record of an intermediate table is compiled to an existing child. This
251  // happens if foreign_selector in parent inline config is set. It will be used by default database row
252  // data providers to set this as value for the foreign_selector field on the intermediate table. One use
253  // case is FAL, where for instance a tt_content parent adds relation to an existing sys_file record and
254  // should set the uid of the existing sys_file record as uid_local - the foreign_selector of this inline
255  // configuration - of the new intermediate sys_file_reference record. Data provider that are called later
256  // will then use this relation to resolve for instance input placeholder relation values.
257  'inlineChildChildUid' => null,
258  // Inline scenario: A localized parent record is handled, so inline parents can have localized children.
259  // This value is set to TRUE if this array represents a default language
260  // child record that was not yet localized.
261  'isInlineDefaultLanguageRecordInLocalizedParentContext' => false,
262  // If set, inline children will be resolved. This is set to FALSE in inline ajax context where new children
263  // are created and existing children don't matter much.
264  'inlineResolveExistingChildren' => true,
265  // @todo - for input placeholder inline to suppress an infinite loop, this *may* become obsolete if
266  // @todo compilation of certain fields is possible
267  'inlineCompileExistingChildren' => true,
268 
269  // @todo: keys below must be handled / further defined
270  'elementBaseName' => '',
271  'tabAndInlineStack' => [],
272  'inlineData' => [],
273  'inlineStructure' => [],
274 
275  // This array of fields will be set as hidden-fields instead of rendered normally!
276  // This is used by EditDocumentController to force some field values if set as "overrideVals" in _GP
277  'overrideValues' => [],
278  // Default values for fields. This is for example used in DatabaseRowInitializeNew data provider to set
279  // fields to specific values if new records are created. Values are often carried around as "defVals" GET
280  // parameters and hand over by controllers to FormEngine using this array. Array structure is
281  // ['aTableName']['aFieldName'] = 'aValue'.
282  'defaultValues' => [],
283 
284  // This array must NOT be set / manipulated by data providers but is instead used by the render part
285  // of FormEngine to add runtime data. Containers and elements add data here which is given to
286  // sub-containers, elements, controls and wizards.
287  'renderData' => [],
288 
289  // A place for non-core, additional, custom data providers to add data. If a data provider needs to add
290  // additional data to the data array that doesn't fit elsewhere, it can place it here to use it in the
291  // render part again. Data in here should be namespaced in a way that it does not collide with other
292  // data providers adding further data here. Using the extension key as array key could be a good idea.
293  'customData' => [],
294  ];
295  }
296 }
‪TYPO3\CMS\Backend\Form
Definition: AbstractNode.php:18
‪TYPO3\CMS\Backend\Form\FormDataCompiler\compile
‪compile(array $initialData, FormDataGroupInterface $formDataGroup)
Definition: FormDataCompiler.php:35
‪TYPO3\CMS\Backend\Form\FormDataCompiler\initializeResultArray
‪initializeResultArray()
Definition: FormDataCompiler.php:111
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Backend\Form\FormDataGroupInterface\compile
‪array compile(array $result)
‪TYPO3\CMS\Backend\Form\FormDataGroupInterface
Definition: FormDataGroupInterface.php:23
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Backend\Form\FormDataCompiler
Definition: FormDataCompiler.php:26