‪TYPO3CMS  9.5
DatabaseRowInitializeNew.php
Go to the documentation of this file.
1 <?php
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 
20 
25 {
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  $result = $this->‪setDefaultValuesFromGetPost($result);
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 
153  protected function ‪setDefaultValuesFromGetPost(array $result)
154  {
155  if (!empty($result['defaultValues'])) {
156  return $result;
157  }
158 
159  $defaultValues = GeneralUtility::_GP('defVals');
160  if (!empty($defaultValues)) {
161  trigger_error(
162  'Default values for new database rows should be set from controller context. Applying default values'
163  . ' via GET/POST parameters is deprecated since 9.2 and will be removed in version 10',
164  E_USER_DEPRECATED
165  );
166  $result['defaultValues'] = $defaultValues;
167  }
168 
169  return $result;
170  }
171 
182  protected function ‪setDefaultsFromInlineRelations(array $result)
183  {
184  if ($result['inlineChildChildUid'] === null) {
185  return $result;
186  }
187  if (!is_int($result['inlineChildChildUid'])) {
188  throw new \UnexpectedValueException(
189  'An inlineChildChildUid is given for table ' . $result['tableName'] . ', but is not an integer',
190  1444434103
191  );
192  }
193  if (!isset($result['inlineParentConfig']['foreign_selector'])) {
194  throw new \UnexpectedValueException(
195  'An inlineChildChildUid is given for table ' . $result['tableName'] . ', but no foreign_selector in inlineParentConfig',
196  1444434102
197  );
198  }
199  $selectorFieldName = $result['inlineParentConfig']['foreign_selector'];
200  if (!isset($result['processedTca']['columns'][$selectorFieldName]['config']['type'])
201  || (
202  $result['processedTca']['columns'][$selectorFieldName]['config']['type'] !== 'select'
203  && $result['processedTca']['columns'][$selectorFieldName]['config']['type'] !== 'group'
204  )
205  ) {
206  throw new \UnexpectedValueException(
207  $selectorFieldName . ' is target type of a foreign_selector field to table ' . $result['tableName'] . ' and must be either a select or group type field',
208  1444434104
209  );
210  }
211 
212  if ($result['inlineChildChildUid']) {
213  $result['databaseRow'][$selectorFieldName] = $result['inlineChildChildUid'];
214  }
215 
216  return $result;
217  }
218 
230  protected function ‪setDefaultsFromInlineParentLanguage(array $result): array
231  {
232  if (!isset($result['inlineParentConfig']['inline']['parentSysLanguageUid'])
233  || empty($result['processedTca']['ctrl']['languageField'])
234  || empty($result['processedTca']['ctrl']['transOrigPointerField'])
235  ) {
236  return $result;
237  }
238 
239  if (!‪MathUtility::canBeInterpretedAsInteger($result['inlineParentConfig']['inline']['parentSysLanguageUid'])) {
240  throw new \UnexpectedValueException(
241  'A sys_language_uid is set from inline parent config but the value is no integer',
242  1490360772
243  );
244  }
245  $parentSysLanguageUid = (int)$result['inlineParentConfig']['inline']['parentSysLanguageUid'];
246  $languageFieldName = $result['processedTca']['ctrl']['languageField'];
247  $result['databaseRow'][$languageFieldName] = $parentSysLanguageUid;
248 
249  return $result;
250  }
251 
258  protected function ‪setDefaultsFromInlineParentUid(array $result): array
259  {
260  $isInlineChild = $result['isInlineChild'] ?? false;
261  $parentField = $result['inlineParentConfig']['foreign_field'] ?? false;
262 
263  if ($isInlineChild && $parentField && !empty($result['inlineParentUid'])) {
264  $result['databaseRow'][$parentField] = $result['inlineParentUid'];
265  }
266 
267  return $result;
268  }
269 
278  protected function ‪setPid(array $result)
279  {
280  // Set pid to vanillaUid. This can be a negative value
281  // if the record is added relative to another record.
282  $result['databaseRow']['pid'] = $result['vanillaUid'];
283 
284  // In case a new inline record is created, the pid can be set to a different value
285  // by pageTsConfig, but not by userTsConfig. This overrides the above pid selection
286  // and forces the pid of new inline children.
287  $tableNameWithDot = $result['tableName'] . '.';
288  if ($result['isInlineChild'] && isset($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'])) {
289  if (!‪MathUtility::canBeInterpretedAsInteger($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'])) {
290  throw new \UnexpectedValueException(
291  'page TSConfig setting TCAdefaults.' . $tableNameWithDot . 'pid must be a number, but given string '
292  . $result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'] . ' can not be interpreted as integer',
293  1461598332
294  );
295  }
296  $result['databaseRow']['pid'] = (int)$result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'];
297  }
298 
299  return $result;
300  }
301 }
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew\setPid
‪array setPid(array $result)
Definition: DatabaseRowInitializeNew.php:278
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:73
‪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\Backend\Form\FormDataProvider\DatabaseRowInitializeNew\setDefaultsFromInlineRelations
‪array setDefaultsFromInlineRelations(array $result)
Definition: DatabaseRowInitializeNew.php:182
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew\setDefaultsFromInlineParentUid
‪array setDefaultsFromInlineParentUid(array $result)
Definition: DatabaseRowInitializeNew.php:258
‪TYPO3\CMS\Backend\Form\FormDataProvider
Definition: AbstractDatabaseRecordProvider.php:2
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew\setDefaultsFromPageTsConfig
‪array setDefaultsFromPageTsConfig(array $result)
Definition: DatabaseRowInitializeNew.php:86
‪TYPO3\CMS\Backend\Form\FormDataProviderInterface
Definition: FormDataProviderInterface.php:22
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew
Definition: DatabaseRowInitializeNew.php:25
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:21
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew\setDefaultsFromInlineParentLanguage
‪array setDefaultsFromInlineParentLanguage(array $result)
Definition: DatabaseRowInitializeNew.php:230
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew\setDefaultsFromDefaultValues
‪array setDefaultsFromDefaultValues(array $result)
Definition: DatabaseRowInitializeNew.php:133
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew\setDefaultsFromUserTsConfig
‪array setDefaultsFromUserTsConfig(array $result)
Definition: DatabaseRowInitializeNew.php:64
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew\setDefaultValuesFromGetPost
‪array setDefaultValuesFromGetPost(array $result)
Definition: DatabaseRowInitializeNew.php:153