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