‪TYPO3CMS  ‪main
DatabaseRowInitializeNew.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 
21 
26 {
34  public function ‪addData(array $result)
35  {
36  if ($result['command'] !== 'new') {
37  return $result;
38  }
39  if (!is_array($result['databaseRow'])) {
40  throw new \UnexpectedValueException(
41  'databaseRow of table ' . $result['tableName'] . ' is not an array',
42  1444431128
43  );
44  }
45 
46  $result = $this->‪setDefaultsFromUserTsConfig($result);
47  $result = $this->‪setDefaultsFromPageTsConfig($result);
48  $result = $this->‪setDefaultsFromNeighborRow($result);
49  $result = $this->‪setDefaultsFromDefaultValues($result);
50  $result = $this->‪setDefaultsFromInlineRelations($result);
51  $result = $this->‪setDefaultsFromInlineParentLanguage($result);
52  $result = $this->‪setDefaultsFromInlineParentUid($result);
53  $result = $this->‪setPid($result);
54 
55  return $result;
56  }
57 
64  protected function ‪setDefaultsFromUserTsConfig(array $result)
65  {
66  $tableNameWithDot = $result['tableName'] . '.';
67  // Apply default values from user typo script "TCAdefaults" if any
68  if (isset($result['userTsConfig']['TCAdefaults.'][$tableNameWithDot])
69  && is_array($result['userTsConfig']['TCAdefaults.'][$tableNameWithDot])
70  ) {
71  foreach ($result['userTsConfig']['TCAdefaults.'][$tableNameWithDot] as $fieldName => $fieldValue) {
72  if (isset($result['processedTca']['columns'][$fieldName])) {
73  $result['databaseRow'][$fieldName] = $fieldValue;
74  }
75  }
76  }
77  return $result;
78  }
79 
86  protected function ‪setDefaultsFromPageTsConfig(array $result)
87  {
88  $tableNameWithDot = $result['tableName'] . '.';
89  if (isset($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot])
90  && is_array($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot])
91  ) {
92  foreach ($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot] as $fieldName => $fieldValue) {
93  if (isset($result['processedTca']['columns'][$fieldName])) {
94  $result['databaseRow'][$fieldName] = $fieldValue;
95  }
96  }
97  }
98  return $result;
99  }
100 
108  protected function ‪setDefaultsFromNeighborRow(array $result)
109  {
110  if (is_array($result['neighborRow'])
111  && !empty($result['processedTca']['ctrl']['useColumnsForDefaultValues'])
112  ) {
113  $defaultColumns = ‪GeneralUtility::trimExplode(',', $result['processedTca']['ctrl']['useColumnsForDefaultValues'], true);
114  foreach ($defaultColumns as $fieldName) {
115  if (isset($result['processedTca']['columns'][$fieldName])
116  && isset($result['neighborRow'][$fieldName])
117  ) {
118  $result['databaseRow'][$fieldName] = $result['neighborRow'][$fieldName];
119  }
120  }
121  }
122  return $result;
123  }
124 
133  protected function ‪setDefaultsFromDefaultValues(array $result)
134  {
135  $tableName = $result['tableName'];
136  $defaultValues = $result['defaultValues'] ?? [];
137  if (isset($defaultValues[$tableName]) && is_array($defaultValues[$tableName])) {
138  foreach ($defaultValues[$tableName] as $fieldName => $fieldValue) {
139  if (isset($result['processedTca']['columns'][$fieldName])) {
140  $result['databaseRow'][$fieldName] = $fieldValue;
141  }
142  }
143  }
144  return $result;
145  }
146 
157  protected function ‪setDefaultsFromInlineRelations(array $result)
158  {
159  if ($result['inlineChildChildUid'] === null) {
160  return $result;
161  }
162  if (!is_int($result['inlineChildChildUid'])) {
163  throw new \UnexpectedValueException(
164  'An inlineChildChildUid is given for table ' . $result['tableName'] . ', but is not an integer',
165  1444434103
166  );
167  }
168  if (!isset($result['inlineParentConfig']['foreign_selector'])) {
169  throw new \UnexpectedValueException(
170  'An inlineChildChildUid is given for table ' . $result['tableName'] . ', but no foreign_selector in inlineParentConfig',
171  1444434102
172  );
173  }
174  $selectorFieldName = $result['inlineParentConfig']['foreign_selector'];
175  $fieldType = (string)($result['processedTca']['columns'][$selectorFieldName]['config']['type'] ?? '');
176  if (!in_array($fieldType, ['select', 'category', 'group'], true)) {
177  throw new \UnexpectedValueException(
178  $selectorFieldName . ' is target type of a foreign_selector field to table ' . $result['tableName'] . ' and must be either a select, category or group type field',
179  1444434104
180  );
181  }
182 
183  if ($result['inlineChildChildUid']) {
184  $result['databaseRow'][$selectorFieldName] = $result['inlineChildChildUid'];
185  }
186 
187  return $result;
188  }
189 
201  protected function ‪setDefaultsFromInlineParentLanguage(array $result): array
202  {
203  if (!isset($result['inlineParentConfig']['inline']['parentSysLanguageUid'])
204  || empty($result['processedTca']['ctrl']['languageField'])
205  || empty($result['processedTca']['ctrl']['transOrigPointerField'])
206  ) {
207  return $result;
208  }
209 
210  if (!‪MathUtility::canBeInterpretedAsInteger($result['inlineParentConfig']['inline']['parentSysLanguageUid'])) {
211  throw new \UnexpectedValueException(
212  'A sys_language_uid is set from inline parent config but the value is no integer',
213  1490360772
214  );
215  }
216  $parentSysLanguageUid = (int)$result['inlineParentConfig']['inline']['parentSysLanguageUid'];
217  $languageFieldName = $result['processedTca']['ctrl']['languageField'];
218  $result['databaseRow'][$languageFieldName] = $parentSysLanguageUid;
219 
220  return $result;
221  }
222 
228  protected function ‪setDefaultsFromInlineParentUid(array $result): array
229  {
230  $isInlineChild = $result['isInlineChild'] ?? false;
231  $parentField = $result['inlineParentConfig']['foreign_field'] ?? false;
232 
233  if ($isInlineChild && $parentField && !empty($result['inlineParentUid'])) {
234  $result['databaseRow'][$parentField] = $result['inlineParentUid'];
235  }
236 
237  return $result;
238  }
239 
248  protected function ‪setPid(array $result)
249  {
250  // Set pid to vanillaUid. This can be a negative value
251  // if the record is added relative to another record.
252  $result['databaseRow']['pid'] = $result['vanillaUid'];
253 
254  // In case a new inline record is created, the pid can be set to a different value
255  // by page TSconfig, but not by user TSconfig. This overrides the above pid selection
256  // and forces the pid of new inline children.
257  $tableNameWithDot = $result['tableName'] . '.';
258  if ($result['isInlineChild'] && isset($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'])) {
259  if (!‪MathUtility::canBeInterpretedAsInteger($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'])) {
260  throw new \UnexpectedValueException(
261  'page TSconfig setting TCAdefaults.' . $tableNameWithDot . 'pid must be a number, but given string '
262  . $result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'] . ' can not be interpreted as integer',
263  1461598332
264  );
265  }
266  $result['databaseRow']['pid'] = (int)$result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'];
267  }
268 
269  return $result;
270  }
271 }
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew\setPid
‪array setPid(array $result)
Definition: DatabaseRowInitializeNew.php:248
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew\setDefaultsFromNeighborRow
‪array setDefaultsFromNeighborRow(array $result)
Definition: DatabaseRowInitializeNew.php:108
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew\addData
‪array addData(array $result)
Definition: DatabaseRowInitializeNew.php:34
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew\setDefaultsFromInlineRelations
‪array setDefaultsFromInlineRelations(array $result)
Definition: DatabaseRowInitializeNew.php:157
‪TYPO3\CMS\Backend\Form\FormDataProvider
Definition: AbstractDatabaseRecordProvider.php:16
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew\setDefaultsFromPageTsConfig
‪array setDefaultsFromPageTsConfig(array $result)
Definition: DatabaseRowInitializeNew.php:86
‪TYPO3\CMS\Backend\Form\FormDataProviderInterface
Definition: FormDataProviderInterface.php:23
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew
Definition: DatabaseRowInitializeNew.php:26
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew\setDefaultsFromInlineParentLanguage
‪array setDefaultsFromInlineParentLanguage(array $result)
Definition: DatabaseRowInitializeNew.php:201
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew\setDefaultsFromDefaultValues
‪array setDefaultsFromDefaultValues(array $result)
Definition: DatabaseRowInitializeNew.php:133
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew\setDefaultsFromInlineParentUid
‪setDefaultsFromInlineParentUid(array $result)
Definition: DatabaseRowInitializeNew.php:228
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew\setDefaultsFromUserTsConfig
‪array setDefaultsFromUserTsConfig(array $result)
Definition: DatabaseRowInitializeNew.php:64