Revert "[BUGFIX] Improve Performance for Inline Elements"
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataProvider / TcaSelectItems.php
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
17 use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
18 use TYPO3\CMS\Core\Utility\MathUtility;
19
20 /**
21 * Resolve select items, set processed item list in processedTca, sanitize and resolve database field
22 */
23 class TcaSelectItems extends AbstractItemProvider implements FormDataProviderInterface
24 {
25 /**
26 * Resolve select items
27 *
28 * @param array $result
29 * @return array
30 * @throws \UnexpectedValueException
31 */
32 public function addData(array $result)
33 {
34 $table = $result['tableName'];
35
36 foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
37 if (empty($fieldConfig['config']['type']) || $fieldConfig['config']['type'] !== 'select') {
38 continue;
39 }
40
41 // Make sure we are only processing supported renderTypes
42 if (!$this->isTargetRenderType($fieldConfig)) {
43 continue;
44 }
45
46 $fieldConfig['config']['items'] = $this->sanitizeItemArray($fieldConfig['config']['items'] ?? [], $table, $fieldName);
47
48 // Resolve "itemsProcFunc"
49 if (!empty($fieldConfig['config']['itemsProcFunc'])) {
50 $fieldConfig['config']['items'] = $this->resolveItemProcessorFunction($result, $fieldName, $fieldConfig['config']['items']);
51 // itemsProcFunc must not be used anymore
52 unset($fieldConfig['config']['itemsProcFunc']);
53 }
54
55 $fieldConfig['config']['maxitems'] = MathUtility::forceIntegerInRange($fieldConfig['config']['maxitems'] ?? 0, 0, 99999);
56 if ($fieldConfig['config']['maxitems'] === 0) {
57 $fieldConfig['config']['maxitems'] = 99999;
58 }
59
60 $fieldConfig['config']['items'] = $this->addItemsFromSpecial($result, $fieldName, $fieldConfig['config']['items']);
61 $fieldConfig['config']['items'] = $this->addItemsFromFolder($result, $fieldName, $fieldConfig['config']['items']);
62 $staticItems = $fieldConfig['config']['items'];
63
64 $fieldConfig['config']['items'] = $this->addItemsFromForeignTable($result, $fieldName, $fieldConfig['config']['items']);
65 // removing items before $dynamicItems and $removedItems have been built results in having them
66 // not populated to the dynamic database row and displayed as "invalid value" in the forms view
67 $fieldConfig['config']['items'] = $this->removeItemsByUserStorageRestriction($result, $fieldName, $fieldConfig['config']['items']);
68
69 $dynamicItems = array_diff_key($fieldConfig['config']['items'], $staticItems);
70 $removedItems = $fieldConfig['config']['items'];
71
72 $fieldConfig['config']['items'] = $this->removeItemsByKeepItemsPageTsConfig($result, $fieldName, $fieldConfig['config']['items']);
73 $fieldConfig['config']['items'] = $this->addItemsFromPageTsConfig($result, $fieldName, $fieldConfig['config']['items']);
74 $fieldConfig['config']['items'] = $this->removeItemsByRemoveItemsPageTsConfig($result, $fieldName, $fieldConfig['config']['items']);
75
76 $fieldConfig['config']['items'] = $this->removeItemsByUserLanguageFieldRestriction($result, $fieldName, $fieldConfig['config']['items']);
77 $fieldConfig['config']['items'] = $this->removeItemsByUserAuthMode($result, $fieldName, $fieldConfig['config']['items']);
78 $fieldConfig['config']['items'] = $this->removeItemsByDoktypeUserRestriction($result, $fieldName, $fieldConfig['config']['items']);
79
80 $removedItems = array_diff_key($removedItems, $fieldConfig['config']['items']);
81
82 // needed to determine the items for invalid values
83 $currentDatabaseValuesArray = $this->processDatabaseFieldValue($result['databaseRow'], $fieldName);
84 $result['databaseRow'][$fieldName] = $currentDatabaseValuesArray;
85
86 $staticValues = $this->getStaticValues($fieldConfig['config']['items'], $dynamicItems);
87 $result['databaseRow'][$fieldName] = $this->processSelectFieldValue($result, $fieldName, $staticValues);
88
89 $fieldConfig['config']['items'] = $this->addInvalidItemsFromDatabase(
90 $result,
91 $table,
92 $fieldName,
93 $fieldConfig,
94 $currentDatabaseValuesArray,
95 $removedItems
96 );
97
98 // Translate labels
99 $fieldConfig['config']['items'] = $this->translateLabels($result, $fieldConfig['config']['items'], $table, $fieldName);
100
101 // Keys may contain table names, so a numeric array is created
102 $fieldConfig['config']['items'] = array_values($fieldConfig['config']['items']);
103
104 $result['processedTca']['columns'][$fieldName] = $fieldConfig;
105 }
106
107 return $result;
108 }
109
110 /**
111 * Add values that are currently listed in the database columns but not in the selectable items list
112 * back to the list.
113 *
114 * @param array $result The current result array.
115 * @param string $table The current table name
116 * @param string $fieldName The current field name
117 * @param array $fieldConf The configuration of the current field.
118 * @param array $databaseValues The item values from the database, can contain invalid items!
119 * @param array $removedItems Items removed by access checks and restrictions, must not be added as invalid values
120 * @return array
121 */
122 public function addInvalidItemsFromDatabase(array $result, $table, $fieldName, array $fieldConf, array $databaseValues, array $removedItems)
123 {
124 // Early return if there are no items or invalid values should not be displayed
125 if (empty($fieldConf['config']['items'])
126 || $fieldConf['config']['renderType'] !== 'selectSingle'
127 || ($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['disableNoMatchingValueElement'] ?? false)
128 || ($fieldConf['config']['disableNoMatchingValueElement'] ?? false)
129 ) {
130 return $fieldConf['config']['items'];
131 }
132
133 $languageService = $this->getLanguageService();
134 $noMatchingLabel = isset($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['noMatchingValue_label'])
135 ? $languageService->sL(trim($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['noMatchingValue_label']))
136 : '[ ' . $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.noMatchingValue') . ' ]';
137
138 $unmatchedValues = array_diff(
139 array_values($databaseValues),
140 array_column($fieldConf['config']['items'], 1),
141 array_column($removedItems, 1)
142 );
143
144 foreach ($unmatchedValues as $unmatchedValue) {
145 $invalidItem = [
146 @sprintf($noMatchingLabel, $unmatchedValue),
147 $unmatchedValue
148 ];
149 array_unshift($fieldConf['config']['items'], $invalidItem);
150 }
151
152 return $fieldConf['config']['items'];
153 }
154
155 /**
156 * Determines whether the current field is a valid target for this DataProvider
157 *
158 * @param array $fieldConfig
159 * @return bool
160 */
161 protected function isTargetRenderType(array $fieldConfig)
162 {
163 return $fieldConfig['config']['renderType'] !== 'selectTree';
164 }
165 }