‪TYPO3CMS  ‪main
TcaTypesShowitem.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 
27 {
33  protected ‪$processedTca;
34 
40  public function ‪addData(array $result)
41  {
42  $this->processedTca = $result['processedTca'];
43 
44  $recordTypeValue = $result['recordTypeValue'];
45 
46  // Handle subtype_value_field, subtypes_addlist, subtypes_excludelist
47  if (!empty($result['processedTca']['types'][$recordTypeValue]['subtype_value_field'])) {
48  $subtypeFieldName = $result['processedTca']['types'][$recordTypeValue]['subtype_value_field'];
49  if (array_key_exists($subtypeFieldName, $result['databaseRow'])) {
50  $subtypeValue = $result['databaseRow'][$subtypeFieldName];
51  $result = $this->‪addFieldsBySubtypeAddList($result, $subtypeFieldName, $subtypeValue, $recordTypeValue);
52  $result = $this->‪removeFieldsBySubtypeExcludeList($result, $subtypeValue, $recordTypeValue);
53  }
54  }
55 
56  // Handling of these parameters is finished. Unset them to not allow other handlers to fiddle with it.
57  // unset does not throw notice, even if not set
58  unset($result['processedTca']['types'][$recordTypeValue]['subtype_value_field']);
59  unset($result['processedTca']['types'][$recordTypeValue]['subtypes_excludelist']);
60  unset($result['processedTca']['types'][$recordTypeValue]['subtypes_addlist']);
61 
62  return $result;
63  }
64 
81  protected function ‪addFieldsBySubtypeAddList(array $result, $subtypeFieldName, $subtypeValue, $recordTypeValue)
82  {
83  if (!empty($this->processedTca['types'][$recordTypeValue]['subtypes_addlist'][$subtypeValue])
84  && is_string($this->processedTca['types'][$recordTypeValue]['subtypes_addlist'][$subtypeValue])
85  ) {
86  $addListString = $this->processedTca['types'][$recordTypeValue]['subtypes_addlist'][$subtypeValue];
87  $addListArray = ‪GeneralUtility::trimExplode(',', $addListString, true);
88  $showItemFieldString = $result['processedTca']['types'][$recordTypeValue]['showitem'];
89  $showItemFieldArray = ‪GeneralUtility::trimExplode(',', $showItemFieldString, true);
90  // The "new" fields should be added after the subtype field itself, so find it
91  foreach ($showItemFieldArray as $index => $fieldConfigurationString) {
92  $found = false;
93  $fieldConfigurationArray = ‪GeneralUtility::trimExplode(';', $fieldConfigurationString);
94  $fieldName = $fieldConfigurationArray[0];
95  if ($fieldName === $subtypeFieldName) {
96  // Found the subtype value field in showitem
97  $found = true;
98  } elseif ($fieldName === '--palette--') {
99  // Try to find subtype value field in palette
100  if (isset($fieldConfigurationArray[2])) {
101  $paletteName = $fieldConfigurationArray[2];
102  if (!empty($this->processedTca['palettes'][$paletteName]['showitem'])) {
103  $paletteFields = ‪GeneralUtility::trimExplode(',', $this->processedTca['palettes'][$paletteName]['showitem'], true);
104  foreach ($paletteFields as $paletteFieldConfiguration) {
105  $paletteFieldConfigurationArray = ‪GeneralUtility::trimExplode(';', $paletteFieldConfiguration);
106  if ($paletteFieldConfigurationArray[0] === $subtypeFieldName) {
107  // Found it in palette
108  $found = true;
109  break;
110  }
111  }
112  }
113  }
114  }
115  if ($found) {
116  // Add fields between subtype field and next element
117  array_splice($showItemFieldArray, $index + 1, 0, $addListArray);
118  break;
119  }
120  }
121  $result['processedTca']['types'][$recordTypeValue]['showitem'] = implode(',', $showItemFieldArray);
122  }
123  return $result;
124  }
125 
141  protected function ‪removeFieldsBySubtypeExcludeList(array $result, $subtypeValue, $recordTypeValue)
142  {
143  if (!empty($this->processedTca['types'][$recordTypeValue]['subtypes_excludelist'][$subtypeValue])
144  && is_string($this->processedTca['types'][$recordTypeValue]['subtypes_excludelist'][$subtypeValue])
145  ) {
146  $removeListString = $this->processedTca['types'][$recordTypeValue]['subtypes_excludelist'][$subtypeValue];
147  $removeListArray = ‪GeneralUtility::trimExplode(',', $removeListString, true);
148  $result = $this->‪removeFields($result, $removeListArray, $recordTypeValue);
149  $result = $this->‪removeFieldsFromPalettes($result, $removeListArray);
150  }
151  return $result;
152  }
153 
162  protected function ‪removeFields(array $result, array $removeListArray, $recordTypeValue)
163  {
164  $newFieldList = [];
165  $showItemFieldString = $result['processedTca']['types'][$recordTypeValue]['showitem'];
166  $showItemFieldArray = ‪GeneralUtility::trimExplode(',', $showItemFieldString, true);
167  foreach ($showItemFieldArray as $fieldConfigurationString) {
168  $fieldConfigurationArray = ‪GeneralUtility::trimExplode(';', $fieldConfigurationString);
169  $fieldName = $fieldConfigurationArray[0];
170  if (!in_array($fieldConfigurationArray[0], $removeListArray, true)
171  // It does not make sense to exclude --palette-- and --div--
172  || $fieldName === '--palette--' || $fieldName === '--div--'
173  ) {
174  $newFieldList[] = $fieldConfigurationString;
175  }
176  }
177  $result['processedTca']['types'][$recordTypeValue]['showitem'] = implode(',', $newFieldList);
178  return $result;
179  }
180 
189  protected function ‪removeFieldsFromPalettes(array $result, $removeListArray)
190  {
191  if (isset($result['processedTca']['palettes']) && is_array($result['processedTca']['palettes'])) {
192  foreach ($result['processedTca']['palettes'] as $paletteName => $paletteArray) {
193  if (!isset($paletteArray['showitem']) || !is_string($paletteArray['showitem'])) {
194  throw new \UnexpectedValueException(
195  'showitem field of palette ' . $paletteName . ' in table ' . $result['tableName'] . ' not found or not a string',
196  1439925240
197  );
198  }
199  $showItemFieldString = $paletteArray['showitem'];
200  $showItemFieldArray = ‪GeneralUtility::trimExplode(',', $showItemFieldString, true);
201  $newFieldList = [];
202  foreach ($showItemFieldArray as $fieldConfigurationString) {
203  $fieldConfigurationArray = ‪GeneralUtility::trimExplode(';', $fieldConfigurationString);
204  $fieldName = $fieldConfigurationArray[0];
205  if (!in_array($fieldConfigurationArray[0], $removeListArray, true)
206  || $fieldName === '--linebreak--'
207  ) {
208  $newFieldList[] = $fieldConfigurationString;
209  }
210  }
211  $result['processedTca']['palettes'][$paletteName]['showitem'] = implode(',', $newFieldList);
212  }
213  }
214  return $result;
215  }
216 }
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaTypesShowitem\addFieldsBySubtypeAddList
‪array addFieldsBySubtypeAddList(array $result, $subtypeFieldName, $subtypeValue, $recordTypeValue)
Definition: TcaTypesShowitem.php:80
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaTypesShowitem
Definition: TcaTypesShowitem.php:27
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaTypesShowitem\addData
‪array addData(array $result)
Definition: TcaTypesShowitem.php:39
‪TYPO3\CMS\Backend\Form\FormDataProvider
Definition: AbstractDatabaseRecordProvider.php:16
‪TYPO3\CMS\Backend\Form\FormDataProviderInterface
Definition: FormDataProviderInterface.php:23
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaTypesShowitem\removeFieldsBySubtypeExcludeList
‪array removeFieldsBySubtypeExcludeList(array $result, $subtypeValue, $recordTypeValue)
Definition: TcaTypesShowitem.php:140
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaTypesShowitem\$processedTca
‪array $processedTca
Definition: TcaTypesShowitem.php:32
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaTypesShowitem\removeFieldsFromPalettes
‪array removeFieldsFromPalettes(array $result, $removeListArray)
Definition: TcaTypesShowitem.php:188
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaTypesShowitem\removeFields
‪array removeFields(array $result, array $removeListArray, $recordTypeValue)
Definition: TcaTypesShowitem.php:161