‪TYPO3CMS  ‪main
ItemsProcessorFunctions.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 
23 
29 {
37  public function ‪populateAvailableLanguagesFromSites(array &$fieldDefinition): void
38  {
39  foreach (GeneralUtility::makeInstance(SiteFinder::class)->getAllSites() as $site) {
40  foreach ($site->getAllLanguages() as $languageId => $language) {
41  if (!isset($fieldDefinition['items'][$languageId])) {
42  $fieldDefinition['items'][$languageId] = [
43  'label' => $language->getTitle(),
44  'value' => $languageId,
45  'icon' => $language->getFlagIdentifier(),
46  'tempTitles' => [],
47  ];
48  } elseif ($fieldDefinition['items'][$languageId]['label'] !== $language->getTitle()) {
49  // Temporarily store different titles
50  $fieldDefinition['items'][$languageId]['tempTitles'][] = $language->getTitle();
51  }
52  }
53  }
54 
55  if (!isset($fieldDefinition['items'][0])) {
56  // Since TcaSiteLanguage has a special behaviour, enforcing the
57  // default language ("0") to be always added to the site configuration,
58  // we have to add it to the available items, in case it is not already
59  // present. This only happens for the first ever created site configuration.
60  $fieldDefinition['items'][] = ['label' => 'Default', 'value' => 0, 'icon' => '', 'tempTitles' => []];
61  }
62 
63  ksort($fieldDefinition['items']);
64 
65  // Build the final language label
66  foreach ($fieldDefinition['items'] as &$language) {
67  $language['label'] .= ' [' . $language['value'] . ']';
68  if ($language['tempTitles'] !== []) {
69  $language['label'] .= ' (' . implode(',', array_unique($language['tempTitles'])) . ')';
70  // Unset the temporary title "storage"
71  unset($language['tempTitles']);
72  }
73  }
74  unset($language);
75 
76  // Add PHP_INT_MAX as last - placeholder - value to allow creation of new records
77  // with the "Create new" button, which is usually not possible in "selector" mode.
78  // Note: The placeholder will never be displayed in the selector.
79  $fieldDefinition['items'] = array_values(
80  array_merge($fieldDefinition['items'], [['label' => 'Placeholder', 'value' => PHP_INT_MAX]])
81  );
82  }
83 
87  public function ‪populateFallbackLanguages(array &$fieldDefinition): void
88  {
89  foreach (GeneralUtility::makeInstance(SiteFinder::class)->getAllSites() as $site) {
90  foreach ($site->getAllLanguages() as $languageId => $language) {
91  if (isset($fieldDefinition['row']['languageId'][0])
92  && (int)$fieldDefinition['row']['languageId'][0] === $languageId
93  ) {
94  // Skip current language id
95  continue;
96  }
97  if (!isset($fieldDefinition['items'][$languageId])) {
98  $fieldDefinition['items'][$languageId] = [
99  'label' => $language->getTitle(),
100  'value' => $languageId,
101  'icon' => $language->getFlagIdentifier(),
102  'tempTitles' => [],
103  ];
104  } elseif ($fieldDefinition['items'][$languageId]['label'] !== $language->getTitle()) {
105  // Temporarily store different titles
106  $fieldDefinition['items'][$languageId]['tempTitles'][] = $language->getTitle();
107  }
108  }
109  }
110  ksort($fieldDefinition['items']);
111 
112  // Build the final language label
113  foreach ($fieldDefinition['items'] as &$language) {
114  if ($language['tempTitles'] !== []) {
115  $language['label'] .= ' (' . implode(',', array_unique($language['tempTitles'])) . ')';
116  // Unset the temporary title "storage"
117  unset($language['tempTitles']);
118  }
119  }
120  unset($language);
121 
122  $fieldDefinition['items'] = array_values($fieldDefinition['items']);
123  }
124 
125  public function ‪populateSiteSets(array &$fieldConfiguration): void
126  {
127  $sets = GeneralUtility::makeInstance(SetCollector::class)->getSetDefinitions();
128  foreach ($sets as $set) {
129  $fieldConfiguration['items'][] = [
130  'label' => $set->label,
131  'value' => $set->name,
132  ];
133  }
134  }
135 }
‪TYPO3\CMS\Backend\Configuration\TCA\ItemsProcessorFunctions\populateAvailableLanguagesFromSites
‪populateAvailableLanguagesFromSites(array &$fieldDefinition)
Definition: ItemsProcessorFunctions.php:37
‪TYPO3\CMS\Backend\Configuration\TCA\ItemsProcessorFunctions
Definition: ItemsProcessorFunctions.php:29
‪TYPO3\CMS\Backend\Configuration\TCA\ItemsProcessorFunctions\populateSiteSets
‪populateSiteSets(array &$fieldConfiguration)
Definition: ItemsProcessorFunctions.php:125
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Backend\Configuration\TCA\ItemsProcessorFunctions\populateFallbackLanguages
‪populateFallbackLanguages(array &$fieldDefinition)
Definition: ItemsProcessorFunctions.php:87
‪TYPO3\CMS\Backend\Configuration\TCA
Definition: ItemsProcessorFunctions.php:18
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Site\Set\SetCollector
Definition: SetCollector.php:24