TYPO3 CMS  TYPO3_7-6
TcaSelectItems.php
Go to the documentation of this file.
1 <?php
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 
18 
23 {
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  // removing items before $dynamicItems and $removedItems have been built results in having them
54  // not populated to the dynamic database row and displayed as "invalid value" in the forms view
55  $fieldConfig['config']['items'] = $this->removeItemsByUserStorageRestriction($result, $fieldName, $fieldConfig['config']['items']);
56 
57  $dynamicItems = array_diff_key($fieldConfig['config']['items'], $staticItems);
58  $removedItems = $fieldConfig['config']['items'];
59 
60  $fieldConfig['config']['items'] = $this->removeItemsByKeepItemsPageTsConfig($result, $fieldName, $fieldConfig['config']['items']);
61  $fieldConfig['config']['items'] = $this->addItemsFromPageTsConfig($result, $fieldName, $fieldConfig['config']['items']);
62  $fieldConfig['config']['items'] = $this->removeItemsByRemoveItemsPageTsConfig($result, $fieldName, $fieldConfig['config']['items']);
63 
64  $fieldConfig['config']['items'] = $this->removeItemsByUserLanguageFieldRestriction($result, $fieldName, $fieldConfig['config']['items']);
65  $fieldConfig['config']['items'] = $this->removeItemsByUserAuthMode($result, $fieldName, $fieldConfig['config']['items']);
66  $fieldConfig['config']['items'] = $this->removeItemsByDoktypeUserRestriction($result, $fieldName, $fieldConfig['config']['items']);
67 
68  $removedItems = array_diff_key($removedItems, $fieldConfig['config']['items']);
69 
70  // Resolve "itemsProcFunc"
71  if (!empty($fieldConfig['config']['itemsProcFunc'])) {
72  $fieldConfig['config']['items'] = $this->resolveItemProcessorFunction($result, $fieldName, $fieldConfig['config']['items']);
73  // itemsProcFunc must not be used anymore
74  unset($fieldConfig['config']['itemsProcFunc']);
75  }
76 
77  // needed to determine the items for invalid values
78  $currentDatabaseValuesArray = $this->processDatabaseFieldValue($result['databaseRow'], $fieldName);
79  $result['databaseRow'][$fieldName] = $currentDatabaseValuesArray;
80 
81  $staticValues = $this->getStaticValues($fieldConfig['config']['items'], $dynamicItems);
82  $result['databaseRow'][$fieldName] = $this->processSelectFieldValue($result, $fieldName, $staticValues);
83 
84  $fieldConfig['config']['items'] = $this->addInvalidItemsFromDatabase(
85  $result,
86  $table,
87  $fieldName,
88  $fieldConfig,
89  $currentDatabaseValuesArray,
90  $removedItems
91  );
92 
93  // Translate labels
94  $fieldConfig['config']['items'] = $this->translateLabels($result, $fieldConfig['config']['items'], $table, $fieldName);
95 
96  // Keys may contain table names, so a numeric array is created
97  $fieldConfig['config']['items'] = array_values($fieldConfig['config']['items']);
98 
99  $result['processedTca']['columns'][$fieldName] = $fieldConfig;
100  }
101 
102  return $result;
103  }
104 
117  public function addInvalidItemsFromDatabase(array $result, $table, $fieldName, array $fieldConf, array $databaseValues, array $removedItems)
118  {
119  // Early return if there are no items or invalid values should not be displayed
120  if (empty($fieldConf['config']['items'])
121  || $fieldConf['config']['renderType'] !== 'selectSingle'
122  || $result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['disableNoMatchingValueElement']
123  || $fieldConf['config']['disableNoMatchingValueElement']
124  ) {
125  return $fieldConf['config']['items'];
126  }
127 
128  $languageService = $this->getLanguageService();
129  $noMatchingLabel = isset($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['noMatchingValue_label'])
130  ? $languageService->sL(trim($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['noMatchingValue_label']))
131  : '[ ' . $languageService->sL('LLL:EXT:lang/locallang_core.xlf:labels.noMatchingValue') . ' ]';
132 
133  $unmatchedValues = array_diff(
134  array_values($databaseValues),
135  array_column($fieldConf['config']['items'], 1),
136  array_column($removedItems, 1)
137  );
138 
139  foreach ($unmatchedValues as $unmatchedValue) {
140  $invalidItem = [
141  @sprintf($noMatchingLabel, $unmatchedValue),
142  $unmatchedValue
143  ];
144  array_unshift($fieldConf['config']['items'], $invalidItem);
145  }
146 
147  return $fieldConf['config']['items'];
148  }
149 
156  protected function isTargetRenderType(array $fieldConfig)
157  {
158  return $fieldConfig['config']['renderType'] !== 'selectTree';
159  }
160 }
removeItemsByUserLanguageFieldRestriction(array $result, $fieldName, array $items)
removeItemsByUserAuthMode(array $result, $fieldName, array $items)
addInvalidItemsFromDatabase(array $result, $table, $fieldName, array $fieldConf, array $databaseValues, array $removedItems)
translateLabels(array $result, array $itemArray, $table, $fieldName)
addItemsFromPageTsConfig(array $result, $fieldName, array $items)
removeItemsByUserStorageRestriction(array $result, $fieldName, array $items)
removeItemsByKeepItemsPageTsConfig(array $result, $fieldName, array $items)
addItemsFromSpecial(array $result, $fieldName, array $items)
removeItemsByRemoveItemsPageTsConfig(array $result, $fieldName, array $items)
removeItemsByDoktypeUserRestriction(array $result, $fieldName, array $items)