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