‪TYPO3CMS  10.4
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  if (!isset($result['processedTca']['columns'][$selectorFieldName]['config']['type'])
177  || (
178  $result['processedTca']['columns'][$selectorFieldName]['config']['type'] !== 'select'
179  && $result['processedTca']['columns'][$selectorFieldName]['config']['type'] !== 'group'
180  )
181  ) {
182  throw new \UnexpectedValueException(
183  $selectorFieldName . ' is target type of a foreign_selector field to table ' . $result['tableName'] . ' and must be either a select or group type field',
184  1444434104
185  );
186  }
187 
188  if ($result['inlineChildChildUid']) {
189  $result['databaseRow'][$selectorFieldName] = $result['inlineChildChildUid'];
190  }
191 
192  return $result;
193  }
194 
206  protected function ‪setDefaultsFromInlineParentLanguage(array $result): array
207  {
208  if (!isset($result['inlineParentConfig']['inline']['parentSysLanguageUid'])
209  || empty($result['processedTca']['ctrl']['languageField'])
210  || empty($result['processedTca']['ctrl']['transOrigPointerField'])
211  ) {
212  return $result;
213  }
214 
215  if (!‪MathUtility::canBeInterpretedAsInteger($result['inlineParentConfig']['inline']['parentSysLanguageUid'])) {
216  throw new \UnexpectedValueException(
217  'A sys_language_uid is set from inline parent config but the value is no integer',
218  1490360772
219  );
220  }
221  $parentSysLanguageUid = (int)$result['inlineParentConfig']['inline']['parentSysLanguageUid'];
222  $languageFieldName = $result['processedTca']['ctrl']['languageField'];
223  $result['databaseRow'][$languageFieldName] = $parentSysLanguageUid;
224 
225  return $result;
226  }
227 
234  protected function ‪setDefaultsFromInlineParentUid(array $result): array
235  {
236  $isInlineChild = $result['isInlineChild'] ?? false;
237  $parentField = $result['inlineParentConfig']['foreign_field'] ?? false;
238 
239  if ($isInlineChild && $parentField && !empty($result['inlineParentUid'])) {
240  $result['databaseRow'][$parentField] = $result['inlineParentUid'];
241  }
242 
243  return $result;
244  }
245 
254  protected function ‪setPid(array $result)
255  {
256  // Set pid to vanillaUid. This can be a negative value
257  // if the record is added relative to another record.
258  $result['databaseRow']['pid'] = $result['vanillaUid'];
259 
260  // In case a new inline record is created, the pid can be set to a different value
261  // by pageTsConfig, but not by userTsConfig. This overrides the above pid selection
262  // and forces the pid of new inline children.
263  $tableNameWithDot = $result['tableName'] . '.';
264  if ($result['isInlineChild'] && isset($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'])) {
265  if (!‪MathUtility::canBeInterpretedAsInteger($result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'])) {
266  throw new \UnexpectedValueException(
267  'page TSConfig setting TCAdefaults.' . $tableNameWithDot . 'pid must be a number, but given string '
268  . $result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'] . ' can not be interpreted as integer',
269  1461598332
270  );
271  }
272  $result['databaseRow']['pid'] = (int)$result['pageTsConfig']['TCAdefaults.'][$tableNameWithDot]['pid'];
273  }
274 
275  return $result;
276  }
277 }
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew\setPid
‪array setPid(array $result)
Definition: DatabaseRowInitializeNew.php:254
‪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:234
‪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\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪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:46
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew\setDefaultsFromInlineParentLanguage
‪array setDefaultsFromInlineParentLanguage(array $result)
Definition: DatabaseRowInitializeNew.php:206
‪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