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