‪TYPO3CMS  10.4
TcaInlineConfiguration.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 
20 
25 {
33  public function ‪addData(array $result)
34  {
35  foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
36  if (empty($fieldConfig['config']['type']) || $fieldConfig['config']['type'] !== 'inline') {
37  continue;
38  }
39 
40  // Throw if an inline field without foreign_table is set
41  if (!isset($fieldConfig['config']['foreign_table'])) {
42  throw new \UnexpectedValueException(
43  'Inline field ' . $fieldName . ' of table ' . $result['tableName'] . ' must have a foreign_table config',
44  1443793404
45  );
46  }
47 
48  $result = $this->‪initializeMinMaxItems($result, $fieldName);
49  $result = $this->‪initializeChildrenLanguage($result, $fieldName);
50  $result = $this->‪initializeAppearance($result, $fieldName);
51  $result = $this->‪addInlineSelectorAndUniqueConfiguration($result, $fieldName);
52  }
53 
54  // If field is set to readOnly, set all fields of the relation to readOnly as well
55  if (isset($result['inlineParentConfig']) && isset($result['inlineParentConfig']['readOnly']) && $result['inlineParentConfig']['readOnly']) {
56  foreach ($result['processedTca']['columns'] as $columnName => $columnConfiguration) {
57  $result['processedTca']['columns'][$columnName]['config']['readOnly'] = true;
58  }
59  }
60 
61  return $result;
62  }
63 
72  protected function ‪initializeMinMaxItems(array $result, $fieldName)
73  {
74  $config = $result['processedTca']['columns'][$fieldName]['config'];
75 
76  $minItems = 0;
77  if (isset($config['minitems'])) {
78  $minItems = ‪MathUtility::forceIntegerInRange($config['minitems'], 0);
79  }
80  $result['processedTca']['columns'][$fieldName]['config']['minitems'] = $minItems;
81 
82  $maxItems = 99999;
83  if (isset($config['maxitems'])) {
84  $maxItems = ‪MathUtility::forceIntegerInRange($config['maxitems'], 1);
85  }
86  $result['processedTca']['columns'][$fieldName]['config']['maxitems'] = $maxItems;
87 
88  return $result;
89  }
90 
99  protected function ‪initializeAppearance(array $result, $fieldName)
100  {
101  $config = $result['processedTca']['columns'][$fieldName]['config'];
102  if (!isset($config['appearance']) || !is_array($config['appearance'])) {
103  // Init appearance if not set
104  $config['appearance'] = [];
105  }
106  // Set the position/appearance of the "Create new record" link
107  if (isset($config['foreign_selector']) && $config['foreign_selector']
108  && (!isset($config['appearance']['useCombination']) || !$config['appearance']['useCombination'])
109  ) {
110  $config['appearance']['levelLinksPosition'] = 'none';
111  } elseif (!isset($config['appearance']['levelLinksPosition'])
112  || !in_array($config['appearance']['levelLinksPosition'], ['top', 'bottom', 'both', 'none'], true)
113  ) {
114  $config['appearance']['levelLinksPosition'] = 'top';
115  }
116  $config['appearance']['showPossibleLocalizationRecords']
117  = isset($config['appearance']['showPossibleLocalizationRecords']) && $config['appearance']['showPossibleLocalizationRecords'];
118  $config['appearance']['showRemovedLocalizationRecords']
119  = isset($config['appearance']['showRemovedLocalizationRecords']) && $config['appearance']['showRemovedLocalizationRecords'];
120  // Defines which controls should be shown in header of each record
121  $enabledControls = [
122  'info' => true,
123  'new' => true,
124  'dragdrop' => true,
125  'sort' => true,
126  'hide' => true,
127  'delete' => true,
128  'localize' => true,
129  ];
130  if (isset($config['appearance']['enabledControls']) && is_array($config['appearance']['enabledControls'])) {
131  $config['appearance']['enabledControls'] = array_merge($enabledControls, $config['appearance']['enabledControls']);
132  } else {
133  $config['appearance']['enabledControls'] = $enabledControls;
134  }
135  $result['processedTca']['columns'][$fieldName]['config'] = $config;
136 
137  return $result;
138  }
139 
153  protected function ‪initializeChildrenLanguage(array $result, $fieldName)
154  {
155  $childTableName = $result['processedTca']['columns'][$fieldName]['config']['foreign_table'];
156 
157  if (empty($result['processedTca']['ctrl']['languageField'])
158  || empty(‪$GLOBALS['TCA'][$childTableName]['ctrl']['languageField'])
159  ) {
160  return $result;
161  }
162 
163  $parentConfig = $result['processedTca']['columns'][$fieldName]['config'];
164 
165  $parentLanguageField = $result['processedTca']['ctrl']['languageField'];
166  if (!isset($parentConfig['inline']['parentSysLanguageUid'])
167  && isset($result['databaseRow'][$parentLanguageField])
168  ) {
169  if (is_array($result['databaseRow'][$parentLanguageField])) {
170  $result['processedTca']['columns'][$fieldName]['config']['inline']['parentSysLanguageUid']
171  = (int)$result['databaseRow'][$parentLanguageField][0];
172  } else {
173  $result['processedTca']['columns'][$fieldName]['config']['inline']['parentSysLanguageUid']
174  = (int)$result['databaseRow'][$parentLanguageField];
175  }
176  }
177 
178  return $result;
179  }
180 
195  protected function ‪addInlineSelectorAndUniqueConfiguration(array $result, $fieldName)
196  {
197  $config = $result['processedTca']['columns'][$fieldName]['config'];
198 
199  // Early return if neither foreign_unique nor foreign_selector are set
200  if (!isset($config['foreign_unique']) && !isset($config['foreign_selector'])) {
201  return $result;
202  }
203 
204  // If both are set, they must point to the same field
205  if (isset($config['foreign_unique']) && isset($config['foreign_selector'])
206  && $config['foreign_unique'] !== $config['foreign_selector']
207  ) {
208  throw new \UnexpectedValueException(
209  'Table ' . $result['tableName'] . ' field ' . $fieldName . ': If both foreign_unique and'
210  . ' foreign_selector are set, they must point to the same field',
211  1444995464
212  );
213  }
214 
215  if (isset($config['foreign_unique'])) {
216  $fieldNameInChildConfiguration = $config['foreign_unique'];
217  } else {
218  $fieldNameInChildConfiguration = $config['foreign_selector'];
219  }
220 
221  // Throw if field name in globals does not exist or is not of type select or group
222  if (!isset(‪$GLOBALS['TCA'][$config['foreign_table']]['columns'][$fieldNameInChildConfiguration]['config']['type'])
223  || (‪$GLOBALS['TCA'][$config['foreign_table']]['columns'][$fieldNameInChildConfiguration]['config']['type'] !== 'select'
224  && ‪$GLOBALS['TCA'][$config['foreign_table']]['columns'][$fieldNameInChildConfiguration]['config']['type'] !== 'group')
225  ) {
226  throw new \UnexpectedValueException(
227  'Table ' . $result['tableName'] . ' field ' . $fieldName . ' points in foreign_selector or foreign_unique'
228  . ' to field ' . $fieldNameInChildConfiguration . ' of table ' . $config['foreign_table'] . ', but this field'
229  . ' is either not defined or is not of type select or group',
230  1444996537
231  );
232  }
233 
234  $selectorOrUniqueConfiguration = [
235  'config' => ‪$GLOBALS['TCA'][$config['foreign_table']]['columns'][$fieldNameInChildConfiguration]['config'],
236  ];
237 
238  // Throw if field is type group, but not internal_type db
239  if ($selectorOrUniqueConfiguration['config']['type'] === 'group'
240  && (!isset($selectorOrUniqueConfiguration['config']['internal_type']) || $selectorOrUniqueConfiguration['config']['internal_type'] !== 'db')
241  ) {
242  throw new \UnexpectedValueException(
243  'Table ' . $result['tableName'] . ' field ' . $fieldName . ' points in foreign_selector or foreign_unique'
244  . ' to field ' . $fieldNameInChildConfiguration . ' of table ' . $config['foreign_table'] . '. This field'
245  . ' is of type group and must be of internal_type db, which is not the case',
246  1444999130
247  );
248  }
249 
250  // Merge overrideChildTca of foreign_selector if given
251  if (isset($config['foreign_selector'], $config['overrideChildTca']['columns'][$config['foreign_selector']]['config'])
252  && is_array($config['overrideChildTca']['columns'][$config['foreign_selector']]['config'])
253  ) {
254  $selectorOrUniqueConfiguration['config'] = array_replace_recursive($selectorOrUniqueConfiguration['config'], $config['overrideChildTca']['columns'][$config['foreign_selector']]['config']);
255  }
256 
257  // Add field name to config for easy access later
258  $selectorOrUniqueConfiguration['fieldName'] = $fieldNameInChildConfiguration;
259 
260  // Add remote table name for easy access later
261  if ($selectorOrUniqueConfiguration['config']['type'] === 'select') {
262  if (!isset($selectorOrUniqueConfiguration['config']['foreign_table'])) {
263  throw new \UnexpectedValueException(
264  'Table ' . $result['tableName'] . ' field ' . $fieldName . ' points in foreign_selector or foreign_unique'
265  . ' to field ' . $fieldNameInChildConfiguration . ' of table ' . $config['foreign_table'] . '. This field'
266  . ' is of type select and must define foreign_table',
267  1445078627
268  );
269  }
270  $foreignTable = $selectorOrUniqueConfiguration['config']['foreign_table'];
271  } else {
272  if (!isset($selectorOrUniqueConfiguration['config']['allowed'])) {
273  throw new \UnexpectedValueException(
274  'Table ' . $result['tableName'] . ' field ' . $fieldName . ' points in foreign_selector or foreign_unique'
275  . ' to field ' . $fieldNameInChildConfiguration . ' of table ' . $config['foreign_table'] . '. This field'
276  . ' is of type select and must define allowed',
277  1445078628
278  );
279  }
280  $foreignTable = $selectorOrUniqueConfiguration['config']['allowed'];
281  }
282  $selectorOrUniqueConfiguration['foreignTable'] = $foreignTable;
283 
284  // If this is a foreign_selector field, mark it as such for data fetching later
285  $selectorOrUniqueConfiguration['isSelector'] = false;
286  if (isset($config['foreign_selector'])) {
287  $selectorOrUniqueConfiguration['isSelector'] = true;
288  }
289 
290  // If this is a foreign_unique field, mark it a such for unique data fetching later
291  $selectorOrUniqueConfiguration['isUnique'] = false;
292  if (isset($config['foreign_unique'])) {
293  $selectorOrUniqueConfiguration['isUnique'] = true;
294  }
295 
296  // Add field configuration to inline configuration
297  $result['processedTca']['columns'][$fieldName]['config']['selectorOrUniqueConfiguration'] = $selectorOrUniqueConfiguration;
298 
299  return $result;
300  }
301 }
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaInlineConfiguration\initializeChildrenLanguage
‪array initializeChildrenLanguage(array $result, $fieldName)
Definition: TcaInlineConfiguration.php:153
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaInlineConfiguration\addData
‪array addData(array $result)
Definition: TcaInlineConfiguration.php:33
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:32
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaInlineConfiguration
Definition: TcaInlineConfiguration.php:25
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaInlineConfiguration\addInlineSelectorAndUniqueConfiguration
‪array addInlineSelectorAndUniqueConfiguration(array $result, $fieldName)
Definition: TcaInlineConfiguration.php:195
‪TYPO3\CMS\Backend\Form\FormDataProvider
Definition: AbstractDatabaseRecordProvider.php:16
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaInlineConfiguration\initializeAppearance
‪array initializeAppearance(array $result, $fieldName)
Definition: TcaInlineConfiguration.php:99
‪TYPO3\CMS\Backend\Form\FormDataProviderInterface
Definition: FormDataProviderInterface.php:23
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaInlineConfiguration\initializeMinMaxItems
‪array initializeMinMaxItems(array $result, $fieldName)
Definition: TcaInlineConfiguration.php:72
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22