[BUGFIX] Notify user about insufficient language configuration
[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\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Database\DatabaseConnection;
20 use TYPO3\CMS\Core\Messaging\FlashMessage;
21 use TYPO3\CMS\Core\Messaging\FlashMessageService;
22 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Lang\LanguageService;
25
26 /**
27 * Fill the "systemLanguageRows" part of the result array
28 */
29 class DatabaseSystemLanguageRows implements FormDataProviderInterface {
30
31 /**
32 * Fetch available system languages and resolve iso code if necessary.
33 *
34 * @todo: This is similar to what TranslationConfigurationProvider->getSystemLanguages() does,
35 * @todo: use the method as soon as bugs have been fixed in there.
36 *
37 * @param array $result
38 * @return array
39 * @throws \UnexpectedValueException
40 */
41 public function addData(array $result) {
42 $database = $this->getDatabase();
43
44 $languageRows = [
45 0 => [
46 'uid' => 0,
47 'title' => 'Default Language',
48 // Default "DEF" is a fallback preparation for flex form iso codes "lDEF"
49 'iso' => 'DEF',
50 ],
51 ];
52
53 $dbRows = $database->exec_SELECTgetRows(
54 'uid,title,language_isocode,static_lang_isocode',
55 'sys_language',
56 'pid=0 AND hidden=0'
57 );
58
59 if ($dbRows === NULL) {
60 throw new \UnexpectedValueException(
61 'Database query error ' . $database->sql_error(),
62 1438170741
63 );
64 }
65
66 $isStaticInfoTablesLoaded = ExtensionManagementUtility::isLoaded('static_info_tables');
67 foreach ($dbRows as $dbRow) {
68 if (!empty($dbRow['language_isocode'])) {
69 $dbRow['iso'] = $dbRow['language_isocode'];
70 } elseif ($isStaticInfoTablesLoaded && !empty($dbRow['static_lang_isocode'])) {
71 GeneralUtility::deprecationLog(
72 'Usage of the field "static_lang_isocode" is discouraged, and will stop working with CMS 8. Use the built-in'
73 . ' language field "language_isocode" in your sys_language records.'
74 );
75 $lg_iso_2 = BackendUtility::getRecord('static_languages', $dbRow['static_lang_isocode'], 'lg_iso_2');
76 if ($lg_iso_2['lg_iso_2']) {
77 $dbRow['iso'] = $lg_iso_2['lg_iso_2'];
78 }
79 } else {
80 // No iso code could be found. This is currently possible in the system but discouraged.
81 // So, code within FormEngine has to be suited to work with an empty iso code. However,
82 // it may impact certain multi language scenarios, so we add a flash message hinting for
83 // incomplete configuration here.
84 // It might be possible to convert this to a non-catchable exception later if
85 // it iso code is enforced on a different layer of the system (tca required + migration wizard).
86 $message = sprintf(
87 $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:error.missingLanguageIsocode'),
88 $dbRow['title'],
89 $dbRow['uid']
90 );
91 /** @var FlashMessage $flashMessage */
92 $flashMessage = GeneralUtility::makeInstance(
93 FlashMessage::class,
94 $message,
95 '',
96 FlashMessage::ERROR
97 );
98 /** @var $flashMessageService FlashMessageService */
99 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
100 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
101 $defaultFlashMessageQueue->enqueue($flashMessage);
102 $dbRow['iso'] = '';
103 }
104 unset($dbRow['language_isocode']);
105 unset($dbRow['static_lang_isocode']);
106 $languageRows[(int)$dbRow['uid']] = $dbRow;
107 }
108
109 $result['systemLanguageRows'] = $languageRows;
110
111 return $result;
112 }
113
114 /**
115 * @return DatabaseConnection
116 */
117 protected function getDatabase() {
118 return $GLOBALS['TYPO3_DB'];
119 }
120
121 /**
122 * @return LanguageService
123 */
124 protected function getLanguageService() {
125 return $GLOBALS['LANG'];
126 }
127 }