‪TYPO3CMS  11.5
TcaLanguage.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
7 /*
8  * This file is part of the TYPO3 CMS project.
9  *
10  * It is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License, either version 2
12  * of the License, or any later version.
13  *
14  * For the full copyright and license information, please read the
15  * LICENSE.txt file that was distributed with this source code.
16  *
17  * The TYPO3 project - inspiring people to share!
18  */
19 
24 
29 {
37  public function ‪addData(array $result): array
38  {
39  $table = $result['tableName'];
40 
41  foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
42  if (!isset($fieldConfig['config']['type']) || $fieldConfig['config']['type'] !== 'language') {
43  continue;
44  }
45 
46  // Save user defined items and reset the field config items array afterwards
47  $userDefinedItems = $this->‪sanitizeItemArray($fieldConfig['config']['items'] ?? [], $table, $fieldName);
48  $fieldConfig['config']['items'] = [];
49 
50  // Initialize site languages to be fetched
51  $siteLanguages = [];
52 
53  if (($result['effectivePid'] ?? 0) === 0 || !($result['site'] ?? null) instanceof ‪Site) {
54  // In case we deal with a pid=0 record or a record on a page outside
55  // of a site config, all languages from all sites should be added.
56  $sites = $this->‪getAllSites();
57  foreach ($sites as $site) {
58  // Add ALL languages from ALL sites
59  foreach ($site->getAllLanguages() as $languageId => $language) {
60  if (isset($siteLanguages[$languageId])) {
61  // Language already provided by another site, just add the label separately
62  $siteLanguages[$languageId]['title'] .= ', ' . $language->getTitle() . ' [Site: ' . $site->getIdentifier() . ']';
63  } else {
64  $siteLanguages[$languageId] = [
65  'title' => $language->getTitle() . ' [Site: ' . $site->getIdentifier() . ']',
66  'flagIconIdentifier' => $language->getFlagIdentifier(),
67  ];
68  }
69  }
70  }
71  ksort($siteLanguages);
72  } elseif (($result['systemLanguageRows'] ?? []) !== []) {
73  // Add system languages available for the current site
74  foreach ($result['systemLanguageRows'] as $languageId => $language) {
75  if ($languageId !== -1) {
76  $siteLanguages[$languageId] = [
77  'title' => $language['title'],
78  'flagIconIdentifier' => $language['flagIconIdentifier'],
79  ];
80  }
81  }
82  }
83 
84  if ($siteLanguages !== []) {
85  // In case siteLanguages are available, add the "site languages" group
86  $fieldConfig['config']['items'] = [
87  [
88  0 => 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.siteLanguages',
89  1 => '--div--',
90  ],
91  ];
92  // Add the fetched site languages to the field config items array
93  foreach ($siteLanguages as $languageId => $language) {
94  $fieldConfig['config']['items'][] = [
95  0 => $language['title'],
96  1 => $languageId,
97  2 => $language['flagIconIdentifier'],
98  ];
99  }
100  }
101 
102  // Add the "special" group for "ALL" and / or user defined items
103  if (($table !== 'pages' && isset($result['systemLanguageRows'][-1])) || $userDefinedItems !== []) {
104  $fieldConfig['config']['items'][] = [
105  0 => 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.specialLanguages',
106  1 => '--div--',
107  ];
108  }
109  // Add "-1" for all TCA records except pages in case the user is allowed to.
110  // The item is added to the "special" group, in order to not provide it as default by accident.
111  if ($table !== 'pages' && isset($result['systemLanguageRows'][-1])) {
112  $fieldConfig['config']['items'][] = [
113  0 => 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.allLanguages',
114  1 => -1,
115  2 => 'flags-multiple',
116  ];
117  }
118 
119  // Add user defined items again so they are in the "special" group
120  $fieldConfig['config']['items'] = array_merge($fieldConfig['config']['items'], $userDefinedItems);
121 
122  // Respect TSconfig options
123  $fieldConfig['config']['items'] = $this->‪removeItemsByKeepItemsPageTsConfig($result, $fieldName, $fieldConfig['config']['items']);
124  $fieldConfig['config']['items'] = $this->‪addItemsFromPageTsConfig($result, $fieldName, $fieldConfig['config']['items']);
125  $fieldConfig['config']['items'] = $this->‪removeItemsByRemoveItemsPageTsConfig($result, $fieldName, $fieldConfig['config']['items']);
126 
127  // In case no items are set at this point, we can write this back and continue with the next column
128  if ($fieldConfig['config']['items'] === []) {
129  $result['processedTca']['columns'][$fieldName] = $fieldConfig;
130  continue;
131  }
132 
133  // Check current database value
134  $currentDatabaseValue = (int)($result['databaseRow'][$fieldName] ?? 0);
135  if (!in_array($currentDatabaseValue, array_map('intval', array_column($fieldConfig['config']['items'], 1)), true)) {
136  // Current value is invalid, so add it with a proper message at the top
137  $fieldConfig['config']['items'] = $this->‪addInvalidItem($result, $table, $fieldName, $currentDatabaseValue, $fieldConfig['config']['items']);
138  }
139 
140  // Reinitialize array keys
141  $fieldConfig['config']['items'] = array_values($fieldConfig['config']['items']);
142 
143  // In case the last element is a divider, remove it
144  if ((string)($fieldConfig['config']['items'][array_key_last($fieldConfig['config']['items'])][1] ?? '') === '--div--') {
145  array_pop($fieldConfig['config']['items']);
146  }
147 
148  // Translate labels
149  $fieldConfig['config']['items'] = $this->‪translateLabels($result, $fieldConfig['config']['items'], $table, $fieldName);
150 
151  // Add icons
152  $fieldConfig['config']['items'] = $this->‪addIconFromAltIcons($result, $fieldConfig['config']['items'], $table, $fieldName);
153 
154  $result['processedTca']['columns'][$fieldName] = $fieldConfig;
155  }
156 
157  return $result;
158  }
159 
160  protected function ‪addInvalidItem(
161  array $result,
162  string $table,
163  string $fieldName,
164  int $invalidValue,
165  array $items
166  ): array {
167  // Early return if there are no items or invalid values should not be displayed
168  if (($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['disableNoMatchingValueElement'] ?? false)
169  || ($result['processedTca']['columns'][$fieldName]['config']['disableNoMatchingValueElement'] ?? false)
170  ) {
171  return $items;
172  }
173 
174  $noMatchingLabel = isset($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['noMatchingValue_label'])
175  ? $this->‪getLanguageService()->‪sL(trim($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['noMatchingValue_label']))
176  : '[ ' . $this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.noMatchingValue') . ' ]';
177 
178  // Add the invalid value at the top
179  array_unshift($items, [@sprintf($noMatchingLabel, $invalidValue), $invalidValue, null]);
180 
181  return $items;
182  }
183 
184  protected function ‪getAllSites(): array
185  {
186  return GeneralUtility::makeInstance(SiteFinder::class)->getAllSites();
187  }
188 }
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractItemProvider\sanitizeItemArray
‪array sanitizeItemArray($itemArray, $tableName, $fieldName)
Definition: AbstractItemProvider.php:1168
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractItemProvider\addItemsFromPageTsConfig
‪array addItemsFromPageTsConfig(array $result, $fieldName, array $items)
Definition: AbstractItemProvider.php:138
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractItemProvider\getLanguageService
‪LanguageService getLanguageService()
Definition: AbstractItemProvider.php:1207
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractItemProvider\addIconFromAltIcons
‪array addIconFromAltIcons(array $result, array $items, string $table, string $fieldName)
Definition: AbstractItemProvider.php:1144
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractItemProvider\translateLabels
‪array translateLabels(array $result, array $itemArray, $table, $fieldName)
Definition: AbstractItemProvider.php:1097
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaLanguage
Definition: TcaLanguage.php:29
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:161
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaLanguage\getAllSites
‪getAllSites()
Definition: TcaLanguage.php:184
‪TYPO3\CMS\Backend\Form\FormDataProvider
Definition: AbstractDatabaseRecordProvider.php:16
‪TYPO3\CMS\Backend\Form\FormDataProviderInterface
Definition: FormDataProviderInterface.php:23
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractItemProvider\removeItemsByKeepItemsPageTsConfig
‪array removeItemsByKeepItemsPageTsConfig(array $result, $fieldName, array $items)
Definition: AbstractItemProvider.php:436
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaLanguage\addInvalidItem
‪addInvalidItem(array $result, string $table, string $fieldName, int $invalidValue, array $items)
Definition: TcaLanguage.php:160
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaLanguage\addData
‪array addData(array $result)
Definition: TcaLanguage.php:37
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractItemProvider
Definition: AbstractItemProvider.php:50
‪TYPO3\CMS\Backend\Form\FormDataProvider\AbstractItemProvider\removeItemsByRemoveItemsPageTsConfig
‪array removeItemsByRemoveItemsPageTsConfig(array $result, $fieldName, array $items)
Definition: AbstractItemProvider.php:469