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