[BUGFIX] FormEngine: Fix keepItems, addItems and removeItems handling
[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\GeneralUtility;
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 $fieldConfig['config']['maxitems'] = $this->sanitizeMaxItems($fieldConfig['config']['maxitems']);
48
49 $fieldConfig['config']['items'] = $this->addItemsFromSpecial($result, $fieldName, $fieldConfig['config']['items']);
50 $fieldConfig['config']['items'] = $this->addItemsFromFolder($result, $fieldName, $fieldConfig['config']['items']);
51 $staticItems = $fieldConfig['config']['items'];
52
53 $fieldConfig['config']['items'] = $this->addItemsFromForeignTable($result, $fieldName, $fieldConfig['config']['items']);
54 $dynamicItems = array_diff_key($fieldConfig['config']['items'], $staticItems);
55
56 $removedItems = $fieldConfig['config']['items'];
57
58 $fieldConfig['config']['items'] = $this->removeItemsByKeepItemsPageTsConfig($result, $fieldName, $fieldConfig['config']['items']);
59 $fieldConfig['config']['items'] = $this->addItemsFromPageTsConfig($result, $fieldName, $fieldConfig['config']['items']);
60 $fieldConfig['config']['items'] = $this->removeItemsByRemoveItemsPageTsConfig($result, $fieldName, $fieldConfig['config']['items']);
61
62 $fieldConfig['config']['items'] = $this->removeItemsByUserLanguageFieldRestriction($result, $fieldName, $fieldConfig['config']['items']);
63 $fieldConfig['config']['items'] = $this->removeItemsByUserAuthMode($result, $fieldName, $fieldConfig['config']['items']);
64 $fieldConfig['config']['items'] = $this->removeItemsByDoktypeUserRestriction($result, $fieldName, $fieldConfig['config']['items']);
65
66 $removedItems = array_diff_key($removedItems, $fieldConfig['config']['items']);
67
68 // Resolve "itemsProcFunc"
69 if (!empty($fieldConfig['config']['itemsProcFunc'])) {
70 $fieldConfig['config']['items'] = $this->resolveItemProcessorFunction($result, $fieldName, $fieldConfig['config']['items']);
71 // itemsProcFunc must not be used anymore
72 unset($fieldConfig['config']['itemsProcFunc']);
73 }
74
75 // needed to determine the items for invalid values
76 $currentDatabaseValuesArray = $this->processDatabaseFieldValue($result['databaseRow'], $fieldName);
77 $result['databaseRow'][$fieldName] = $currentDatabaseValuesArray;
78
79 $staticValues = $this->getStaticValues($fieldConfig['config']['items'], $dynamicItems);
80 $result['databaseRow'][$fieldName] = $this->processSelectFieldValue($result, $fieldName, $staticValues);
81
82 $fieldConfig['config']['items'] = $this->addInvalidItemsFromDatabase(
83 $result,
84 $table,
85 $fieldName,
86 $fieldConfig,
87 $currentDatabaseValuesArray,
88 $removedItems
89 );
90
91 // Translate labels
92 $fieldConfig['config']['items'] = $this->translateLabels($result, $fieldConfig['config']['items'], $table, $fieldName);
93
94 // Keys may contain table names, so a numeric array is created
95 $fieldConfig['config']['items'] = array_values($fieldConfig['config']['items']);
96
97 $result['processedTca']['columns'][$fieldName] = $fieldConfig;
98 }
99
100 return $result;
101 }
102
103 /**
104 * Add values that are currently listed in the database columns but not in the selectable items list
105 * back to the list.
106 *
107 * @param array $result The current result array.
108 * @param string $table The current table name
109 * @param string $fieldName The current field name
110 * @param array $fieldConf The configuration of the current field.
111 * @param array $databaseValues The item values from the database, can contain invalid items!
112 * @param array $removedItems Items removed by access checks and restrictions, must not be added as invalid values
113 * @return array
114 */
115 public function addInvalidItemsFromDatabase(array $result, $table, $fieldName, array $fieldConf, array $databaseValues, array $removedItems)
116 {
117 // Early return if there are no items or invalid values should not be displayed
118 if (empty($fieldConf['config']['items'])
119 || $fieldConf['config']['renderType'] !== 'selectSingle'
120 || $result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['disableNoMatchingValueElement']
121 || $fieldConf['config']['disableNoMatchingValueElement']
122 ) {
123 return $fieldConf['config']['items'];
124 }
125
126 $languageService = $this->getLanguageService();
127 $noMatchingLabel = isset($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['noMatchingValue_label'])
128 ? $languageService->sL(trim($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['noMatchingValue_label']))
129 : '[ ' . $languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.noMatchingValue') . ' ]';
130
131 $unmatchedValues = array_diff(
132 array_values($databaseValues),
133 array_column($fieldConf['config']['items'], 1),
134 array_column($removedItems, 1)
135 );
136
137 foreach ($unmatchedValues as $unmatchedValue) {
138 $invalidItem = [
139 @sprintf($noMatchingLabel, $unmatchedValue),
140 $unmatchedValue
141 ];
142 array_unshift($fieldConf['config']['items'], $invalidItem);
143 }
144
145 return $fieldConf['config']['items'];
146 }
147
148 /**
149 * Determines whether the current field is a valid target for this DataProvider
150 *
151 * @param array $fieldConfig
152 * @return bool
153 */
154 protected function isTargetRenderType(array $fieldConfig)
155 {
156 return in_array(
157 $fieldConfig['config']['renderType'],
158 ['selectSingle', 'selectSingleBox', 'selectCheckBox', 'selectMultipleSideBySide'],
159 true
160 );
161 }
162 }