Passed
Push — master ( 03f78e...954a3f )
by
unknown
12:06
created

TcaLanguage::getAllSites()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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