[FEATURE] Show only sys_languages in Backend which are available
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataProvider / DatabaseSystemLanguageRows.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\FormDataProvider;
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
17 use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
18 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
19 use TYPO3\CMS\Core\Exception\SiteNotFoundException;
20 use TYPO3\CMS\Core\Localization\LanguageService;
21 use TYPO3\CMS\Core\Messaging\FlashMessage;
22 use TYPO3\CMS\Core\Messaging\FlashMessageService;
23 use TYPO3\CMS\Core\Site\PseudoSiteFinder;
24 use TYPO3\CMS\Core\Site\SiteFinder;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27 /**
28 * Fill the "systemLanguageRows" part of the result array
29 */
30 class DatabaseSystemLanguageRows implements FormDataProviderInterface
31 {
32 /**
33 * Fetch available system languages and resolve iso code if necessary.
34 *
35 * @param array $result
36 * @return array
37 * @throws \UnexpectedValueException
38 */
39 public function addData(array $result)
40 {
41 $pageIdDefaultLanguage = $result['defaultLanguagePageRow']['uid'] ?? $result['effectivePid'];
42 try {
43 $site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($pageIdDefaultLanguage);
44 } catch (SiteNotFoundException $e) {
45 // Check for a pseudo site
46 $site = GeneralUtility::makeInstance(PseudoSiteFinder::class)->getSiteByPageId($pageIdDefaultLanguage);
47 }
48 $languages = $site->getAvailableLanguages($this->getBackendUser(), true, $pageIdDefaultLanguage);
49
50 $languageRows = [];
51 foreach ($languages as $language) {
52 $languageId = $language->getLanguageId();
53 if ($languageId > 0) {
54 $iso = $language->getTwoLetterIsoCode();
55 } else {
56 $iso = 'DEF';
57 }
58 $languageRows[$languageId] = [
59 'uid' => $languageId,
60 'title' => $language->getTitle(),
61 'iso' => $iso,
62 'flagIconIdentifier' => $language->getFlagIdentifier()
63 ];
64
65 if (empty($iso)) {
66 // No iso code could be found. This is currently possible in the system but discouraged.
67 // So, code within FormEngine has to be suited to work with an empty iso code. However,
68 // it may impact certain multi language scenarios, so we add a flash message hinting for
69 // incomplete configuration here.
70 // It might be possible to convert this to a non-catchable exception later if
71 // it iso code is enforced on a different layer of the system (tca required + migration wizard).
72 // @todo: This could be relaxed again if flex form language handling is extracted,
73 // @todo: since the rest of the FormEngine code does not rely on iso code?
74 $message = sprintf(
75 $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:error.missingLanguageIsocode'),
76 $language->getTwoLetterIsoCode(),
77 $languageId
78 );
79 /** @var FlashMessage $flashMessage */
80 $flashMessage = GeneralUtility::makeInstance(
81 FlashMessage::class,
82 $message,
83 '',
84 FlashMessage::ERROR
85 );
86 /** @var $flashMessageService FlashMessageService */
87 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
88 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
89 $defaultFlashMessageQueue->enqueue($flashMessage);
90 }
91 }
92 $result['systemLanguageRows'] = $languageRows;
93 return $result;
94 }
95
96 /**
97 * @return LanguageService
98 */
99 protected function getLanguageService(): LanguageService
100 {
101 return $GLOBALS['LANG'];
102 }
103
104 /**
105 * @return BackendUserAuthentication
106 */
107 protected function getBackendUser(): BackendUserAuthentication
108 {
109 return $GLOBALS['BE_USER'];
110 }
111 }