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