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