acb0441dd82095e8b6ee692501f3be21dc94a364
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Context / LanguageAspectFactory.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Context;
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 use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * A simple factory to create a language aspect.
23 */
24 class LanguageAspectFactory
25 {
26 /**
27 * Create a language aspect based on TypoScript settings, which we know:
28 *
29 * config.sys_language_uid
30 * config.sys_language_mode
31 * config.sys_language_overlay
32 *
33 * @param array $config
34 * @return LanguageAspect
35 */
36 public static function createFromTypoScript(array $config): LanguageAspect
37 {
38 // Get values from TypoScript, if not set before
39 $languageId = (int)($config['sys_language_uid'] ?? 0);
40 $fallbacks = GeneralUtility::trimExplode(';', $config['sys_language_mode'] ?? '');
41 $fallbackMode = null;
42 if (isset($fallbacks[0])) {
43 $fallbackMode = $fallbacks[0];
44 }
45 $fallbackOrder = null;
46 if (isset($fallbacks[1])) {
47 $fallbackOrder = $fallbacks[1];
48 }
49
50 // Page resolving
51 switch ($fallbackMode) {
52 case 'strict':
53 $fallBackOrder = [];
54 break;
55 // Ignore anything if a page cannot be found, and resolve pageId=0 instead.
56 case 'ignore':
57 $fallBackOrder = [-1];
58 break;
59 case 'fallback':
60 case 'content_fallback':
61 if (!empty($fallbackOrder)) {
62 $fallBackOrder = GeneralUtility::trimExplode(',', $fallbackOrder);
63 // no strict typing explictly done here
64 if (!in_array(0, $fallBackOrder) && !in_array('pageNotFound', $fallBackOrder)) {
65 $fallBackOrder[] = 'pageNotFound';
66 }
67 } else {
68 $fallBackOrder = [0];
69 }
70 break;
71 case '':
72 $fallBackOrder = ['off'];
73 break;
74 default:
75 $fallBackOrder = [0];
76 }
77
78 // Content fetching
79 switch ((string)($config['sys_language_overlay'] ?? '')) {
80 case '1':
81 $overlayType = LanguageAspect::OVERLAYS_MIXED;
82 break;
83 case '0':
84 $overlayType = LanguageAspect::OVERLAYS_OFF;
85 break;
86 case 'hideNonTranslated':
87 default:
88 $overlayType = LanguageAspect::OVERLAYS_ON_WITH_FLOATING;
89 }
90
91 return GeneralUtility::makeInstance(LanguageAspect::class, $languageId, $languageId, $overlayType, $fallBackOrder);
92 }
93
94 /**
95 * Site Languages always run with overlays + floating records.
96 *
97 * @param SiteLanguage $language
98 * @return LanguageAspect
99 */
100 public static function createFromSiteLanguage(SiteLanguage $language): LanguageAspect
101 {
102 $languageId = $language->getLanguageId();
103 $fallbackType = $language->getFallbackType();
104 if ($fallbackType === 'fallback') {
105 $fallbackOrder = $language->getFallbackLanguageIds();
106 $fallbackOrder[] = 'pageNotFound';
107 } elseif ($fallbackType === 'strict') {
108 $fallbackOrder = [];
109 } else {
110 $fallbackOrder = [0];
111 }
112
113 return GeneralUtility::makeInstance(LanguageAspect::class, $languageId, $languageId, LanguageAspect::OVERLAYS_ON_WITH_FLOATING, $fallbackOrder);
114 }
115 }