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