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