[BUGFIX] FormEngine: Custom functions
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataCompiler.php
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
17 /**
18 * Create and return a defined array of data ready to be used by the
19 * container / element render part of FormEngine
20 */
21 class FormDataCompiler
22 {
23 /**
24 * Data group that provides data
25 *
26 * @var FormDataGroupInterface
27 */
28 protected $formDataGroup;
29
30 /**
31 * List of top level array elements to be unset from
32 * result array before final result is returned.
33 *
34 * @var array
35 */
36 protected $removeKeysFromFinalResultArray = [
37 ];
38
39 /**
40 * Get form data group injected
41 *
42 * @param FormDataGroupInterface $formDataGroup
43 */
44 public function __construct(FormDataGroupInterface $formDataGroup)
45 {
46 $this->formDataGroup = $formDataGroup;
47 }
48
49 /**
50 * Main entry method maps given data input array and sanitizes some
51 * crucial input parameters and calls compile on FormDataGroupInterface.
52 *
53 * @param array $initialData Initial set of data to map into result array
54 * @return array Result with data
55 * @throws \InvalidArgumentException
56 * @throws \UnexpectedValueException
57 */
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 $resultKeysAfterFormDataGroup = array_keys($result);
109
110 if ($resultKeysAfterFormDataGroup !== $resultKeysBeforeFormDataGroup) {
111 throw new \UnexpectedValueException(
112 'Data group provider must not change result key list',
113 1438079402
114 );
115 }
116
117 // Remove some data elements form result that are data provider internal and should
118 // not be exposed to calling object.
119 foreach ($this->removeKeysFromFinalResultArray as $key) {
120 unset($result[$key]);
121 }
122
123 return $result;
124 }
125
126 /**
127 * @return array
128 */
129 protected function initializeResultArray()
130 {
131 return [
132 // Either "edit" or "new"
133 'command' => '',
134 // Table name of the handled row
135 'tableName' => '',
136 // Forced integer of otherwise not changed uid of the record, meaning of value depends on context (new / edit)
137 // * If $command is "edit"
138 // ** $vanillaUid is a positive integer > 0 pointing to the record in the table
139 // * If $command is "new":
140 // ** If $vanillaUid > 0, it is the uid of a page the record should be added at
141 // ** If $vanillaUid < 0, it is the uid of a record in the same table after which the new record should be added
142 // ** If $vanillaUid = 0, a new record is added on page 0
143 'vanillaUid' => 0,
144 // Url to return to
145 'returnUrl' => null,
146 // Title of the handled record.
147 'recordTitle' => '',
148 // Parent page record is either the full row of the parent page the record is located at or should
149 // be added to, or it is NULL, if a record is added or edited below the root page node.
150 'parentPageRow' => null,
151 // Holds the "neighbor" row if incoming vanillaUid is negative and record creation is relative to a row of the same table.
152 'neighborRow' => null,
153 // For "new" this is the fully initialized row with defaults
154 // The database row. For "edit" fixVersioningPid() was applied already.
155 // @todo: rename to valueStructure or handledData or similar
156 'databaseRow' => [],
157 // The "effective" page uid we're working on. This is the uid of a page if a page is edited, or the uid
158 // of the parent page if a page or other record is added, or 0 if a record is added or edited below root node.
159 'effectivePid' => 0,
160 // Rootline of page the record that is handled is located at as created by BackendUtility::BEgetRootline()
161 'rootline' => [],
162 // For "edit", this is the permission bitmask of the page that is edited, or of the page a record is located at
163 // For "new", this is the permission bitmask of the page the record is added to
164 // @todo: Remove if not needed on a lower level
165 'userPermissionOnPage' => 0,
166 // Full user TsConfig
167 'userTsConfig' => [],
168 // Full page TSConfig of the page that is edited or of the parent page if a record is added.
169 // This includes any defaultPageTSconfig and is merged with user TsConfig page. section. After type
170 // of handled record was determined, record type specific settings [TCEFORM.][tableName.][field.][types.][type.]
171 // are merged into [TCEFORM.][tableName.][field.]. Array keys still contain the concatenation dots.
172 'pageTsConfig' => [],
173 // Not changed TCA of parent page row if record is edited or added below a page and not root node
174 'vanillaParentPageTca' => null,
175 // List of available system languages. Array key is the system language uid, value array
176 // contains details of the record, with iso code resolved. Key is the sys_language_uid uid.
177 'systemLanguageRows' => [],
178 // If the page that is handled has "page_language_overlay" records (page has localizations in
179 // different languages), then this array holds those rows.
180 'pageLanguageOverlayRows' => [],
181 // If the handled row is a localized row, this entry hold the default language row array
182 'defaultLanguageRow' => null,
183 // If the handled row is a localized row and a transOrigDiffSourceField is defined, this
184 // is the unserialized version of it. The diff source field is basically a shadow version
185 // of the default language record at the time when the language overlay record was created.
186 // This is used later to compare the default record with this content to show a "diff" if
187 // the default language record changed meanwhile.
188 'defaultLanguageDiffRow' => null,
189 // With userTS options.additionalPreviewLanguages set, field values of additional languages
190 // can be shown. This array holds those additional language records, Array key is sys_language_uid.
191 'additionalLanguageRows' => [],
192 // The tca record type value of the record. Forced to string, there can be "named" type values.
193 'recordTypeValue' => '0',
194 // TCA of table with processed fields. After processing, this array contains merged and resolved
195 // array data, items were resolved, only used types are set, renderTypes are set.
196 'processedTca' => [],
197 // List of columns to be processed by data provider. Array value is the column name.
198 'columnsToProcess' => [],
199 // If set to TRUE, no wizards are calculated and rendered later
200 'disabledWizards' => false,
201
202 // Flex form field data handling is done in a separated FormDataCompiler instance. The full databaseRow
203 // of the record this flex form is embedded in is transferred in case features like single fields
204 // itemsProcFunc need to have this data at hand to do their job.
205 'flexParentDatabaseRow' => [],
206
207 // BackendUser->uc['inlineView'] - This array holds status of expand / collapsed inline items
208 // @todo: better documentation of nesting behaviour and bug fixing in this area
209 'inlineExpandCollapseStateArray' => [],
210 // The "entry" pid for inline records. Nested inline records can potentially hang around on different
211 // pid's, but the entry pid is needed for AJAX calls, so that they would know where the action takes
212 // place on the page structure.
213 'inlineFirstPid' => null,
214 // The "config" section of an inline parent, prepared and sanitized by TcaInlineConfiguration provider
215 'inlineParentConfig' => [],
216 // Flag that is enabled if a records is child of an inline parent
217 'isInlineChild' => false,
218 // Flag if an inline child is expanded so that additional fields need to be rendered
219 'isInlineChildExpanded' => false,
220 // Flag if the inline is in an ajax context that wants to expand the element
221 'isInlineAjaxOpeningContext' => false,
222 // Uid of the direct parent of the inline element. Handled as string since it may be a "NEW123" string
223 'inlineParentUid' => '',
224 // Table name of the direct parent of the inline element
225 'inlineParentTableName' => '',
226 // Field name of the direct parent of the inline element
227 'inlineParentFieldName' => '',
228 // Uid of the top most parent element. Handled as string since it may be a "NEW123" string
229 'inlineTopMostParentUid' => '',
230 // Table name of the top most parent element
231 'inlineTopMostParentTableName' => '',
232 // Field name of the top most parent element
233 'inlineTopMostParentFieldName' => '',
234
235 // If is on symetric side of an inline child parent reference.
236 // symmetric side can be achieved in case of an mm relation to the same table. If record A has a relation
237 // to record B, the symmetric side is set in case that record B gets edited.
238 // Record A (table1) <=> mm <=> Record B (table1)
239 'isOnSymmetricSide' => false,
240
241 // Uid of a "child-child" if a new record of an intermediate table is compiled to an existing child. This
242 // happens if foreign_selector in parent inline config is set. It will be used by default database row
243 // data providers to set this as value for the foreign_selector field on the intermediate table. One use
244 // case is FAL, where for instance a tt_content parent adds relation to an existing sys_file record and
245 // should set the uid of the existing sys_file record as uid_local - the foreign_selector of this inline
246 // configuration - of the new intermediate sys_file_reference record. Data provider that are called later
247 // will then use this relation to resolve for instance input placeholder relation values.
248 'inlineChildChildUid' => null,
249 // Inline scenario: A localized parent record is handled and localizationMode is set to "select", so inline
250 // parents can have localized childen. This value is set to TRUE if this array represents a default language
251 // child record that was not yet localized.
252 'isInlineDefaultLanguageRecordInLocalizedParentContext' => false,
253 // If set, inline children will be resolved. This is set to FALSE in inline ajax context where new children
254 // are created and existing children don't matter much.
255 'inlineResolveExistingChildren' => true,
256 // @todo - for input placeholder inline to suppress an infinite loop, this *may* become obsolete if
257 // @todo compilation of certain fields is possible
258 'inlineCompileExistingChildren' => true,
259
260 // @todo: keys below must be handled / further defined
261 'elementBaseName' => '',
262 'flexFormFieldIdentifierPrefix' => 'ID',
263 'tabAndInlineStack' => [],
264 'inlineData' => [],
265 'inlineStructure' => [],
266 // This array of fields will be set as hidden-fields instead of rendered normally!
267 // This is used by EditDocumentController to force some field values if set as "overrideVals" in _GP
268 'overrideValues' => [],
269 ];
270 }
271 }