Passed
Push — master ( fb6744...da9c6d )
by
unknown
17:04
created

populateFallbackLanguages()   B

Complexity

Conditions 9
Paths 18

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 36
rs 8.0555
cc 9
nc 18
nop 1
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
18
namespace TYPO3\CMS\Backend\Configuration\TCA;
19
20
use TYPO3\CMS\Core\Site\SiteFinder;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23
/**
24
 * This class provides items processor functions for the usage in TCA definition
25
 * @internal
26
 */
27
class ItemsProcessorFunctions
28
{
29
    /**
30
     * Return languages found in already existing site configurations,
31
     * sorted by their value. In case the same language is used with
32
     * different titles, they will be added to the items label field.
33
     * Additionally, a placeholder value is added to allow the creation
34
     * of new site languages.
35
     */
36
    public function populateAvailableLanguagesFromSites(array &$fieldDefinition): void
37
    {
38
        foreach (GeneralUtility::makeInstance(SiteFinder::class)->getAllSites() as $site) {
39
            foreach ($site->getAllLanguages() as $languageId => $language) {
40
                if (!isset($fieldDefinition['items'][$languageId])) {
41
                    $fieldDefinition['items'][$languageId] = [
42
                        $language->getTitle(),
43
                        $languageId,
44
                        $language->getFlagIdentifier(),
45
                        []
46
                    ];
47
                } elseif ($fieldDefinition['items'][$languageId][0] !== $language->getTitle()) {
48
                    // Temporarily store different titles
49
                    $fieldDefinition['items'][$languageId][3][] = $language->getTitle();
50
                }
51
            }
52
        }
53
54
        if (!isset($fieldDefinition['items'][0])) {
55
            // Since TcaSiteLanguage has a special behaviour, enforcing the
56
            // default language ("0") to be always added to the site configuration,
57
            // we have to add it to the available items, in case it is not already
58
            // present. This only happens for the first ever created site configuration.
59
            $fieldDefinition['items'][] = ['Default', 0, '', []];
60
        }
61
62
        ksort($fieldDefinition['items']);
63
64
        // Build the final language label
65
        foreach ($fieldDefinition['items'] as &$language) {
66
            $language[0] .= ' [' . $language[1] . ']';
67
            if ($language[3] !== []) {
68
                $language[0] .= ' (' . implode(',', array_unique($language[3])) . ')';
69
                // Unset the temporary title "storage"
70
                unset($language[3]);
71
            }
72
        }
73
        unset($language);
74
75
        // Add PHP_INT_MAX as last - placeholder - value to allow creation of new records
76
        // with the "Create new" button, which is usually not possible in "selector" mode.
77
        // Note: The placeholder will never be displayed in the selector.
78
        $fieldDefinition['items'] = array_values(
79
            array_merge($fieldDefinition['items'], [['Placeholder', PHP_INT_MAX, '']])
80
        );
81
    }
82
83
    /**
84
     * Return language items for use in site_languages.fallbacks
85
     *
86
     * @param array $fieldDefinition
87
     */
88
    public function populateFallbackLanguages(array &$fieldDefinition): void
89
    {
90
        foreach (GeneralUtility::makeInstance(SiteFinder::class)->getAllSites() as $site) {
91
            foreach ($site->getAllLanguages() as $languageId => $language) {
92
                if (isset($fieldDefinition['row']['languageId'][0])
93
                    && (int)$fieldDefinition['row']['languageId'][0] === $languageId
94
                ) {
95
                    // Skip current language id
96
                    continue;
97
                }
98
                if (!isset($fieldDefinition['items'][$languageId])) {
99
                    $fieldDefinition['items'][$languageId] = [
100
                        $language->getTitle(),
101
                        $languageId,
102
                        $language->getFlagIdentifier(),
103
                        []
104
                    ];
105
                } elseif ($fieldDefinition['items'][$languageId][0] !== $language->getTitle()) {
106
                    // Temporarily store different titles
107
                    $fieldDefinition['items'][$languageId][3][] = $language->getTitle();
108
                }
109
            }
110
        }
111
        ksort($fieldDefinition['items']);
112
113
        // Build the final language label
114
        foreach ($fieldDefinition['items'] as &$language) {
115
            if ($language[3] !== []) {
116
                $language[0] .= ' (' . implode(',', array_unique($language[3])) . ')';
117
                // Unset the temporary title "storage"
118
                unset($language[3]);
119
            }
120
        }
121
        unset($language);
122
123
        $fieldDefinition['items'] = array_values($fieldDefinition['items']);
124
    }
125
}
126