TYPO3  7.6
DatabaseRowInitializeNew.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Backend\Form\FormDataProvider;
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 
19 
24 {
33  public function addData(array $result)
34  {
35  if ($result['command'] !== 'new') {
36  return $result;
37  }
38  if (!is_array($result['databaseRow'])) {
39  throw new \UnexpectedValueException(
40  'databaseRow of table ' . $result['tableName'] . ' is not an array',
41  1444431128
42  );
43  }
44 
45  $result = $this->setDefaultsFromUserTsConfig($result);
46  $result = $this->setDefaultsFromPageTsConfig($result);
47  $result = $this->setDefaultsFromNeighborRow($result);
48  $result = $this->setDefaultsFromDevVals($result);
49  $result = $this->setDefaultsFromInlineRelations($result);
50 
51  // Set pid to vanillaUid. This means, it *can* be negative, if the record is added relative to another record
52  // @todo: For inline records it should be possible to set the pid here via TCAdefaults, but
53  // @todo: those values would be overwritten by this 'pid' setter
54  $result['databaseRow']['pid'] = $result['vanillaUid'];
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 
136  protected function setDefaultsFromDevVals(array $result)
137  {
138  $tableName = $result['tableName'];
139  $defaultValuesFromGetPost = GeneralUtility::_GP('defVals');
140  if (isset($defaultValuesFromGetPost[$tableName])
141  && is_array($defaultValuesFromGetPost[$tableName])
142  ) {
143  foreach ($defaultValuesFromGetPost[$tableName] as $fieldName => $fieldValue) {
144  if (isset($result['processedTca']['columns'][$fieldName])) {
145  $result['databaseRow'][$fieldName] = $fieldValue;
146  }
147  }
148  }
149  return $result;
150  }
151 
162  protected function setDefaultsFromInlineRelations(array $result)
163  {
164  if ($result['inlineChildChildUid'] === null) {
165  return $result;
166  }
167  if (!is_int($result['inlineChildChildUid'])) {
168  throw new \UnexpectedValueException(
169  'An inlineChildChildUid is given for table ' . $result['tableName'] . ', but is not an integer',
170  1444434103
171  );
172  }
173  if (!isset($result['inlineParentConfig']['foreign_selector'])) {
174  throw new \UnexpectedValueException(
175  'An inlineChildChildUid is given for table ' . $result['tableName'] . ', but no foreign_selector in inlineParentConfig',
176  1444434102
177  );
178  }
179  $selectorFieldName = $result['inlineParentConfig']['foreign_selector'];
180  if (!isset($result['processedTca']['columns'][$selectorFieldName]['config']['type'])
181  || ($result['processedTca']['columns'][$selectorFieldName]['config']['type'] !== 'select'
182  && $result['processedTca']['columns'][$selectorFieldName]['config']['type'] !== 'group'
183  )
184  ) {
185  throw new \UnexpectedValueException(
186  $selectorFieldName . ' is target type of a foreign_selector field to table ' . $result['tableName'] . ' and must be either a select or group type field',
187  1444434104
188  );
189  }
190 
191  if ($result['inlineChildChildUid']) {
192  $result['databaseRow'][$selectorFieldName] = $result['inlineChildChildUid'];
193  }
194 
195  return $result;
196  }
197 }