‪TYPO3CMS  9.5
SiteLanguageAccessorTrait.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
3 
5 
6 /*
7  * This file is part of the TYPO3 CMS project.
8  *
9  * It is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License, either version 2
11  * of the License, or any later version.
12  *
13  * For the full copyright and license information, please read the
14  * LICENSE.txt file that was distributed with this source code.
15  *
16  * The TYPO3 project - inspiring people to share!
17  */
18 
23 
25 {
27 
31  protected $languageAspect;
32 
41  protected function resolveLanguageFallback(array $results, ?string $languageFieldName, ?array $languageIds): ?array
42  {
43  if ($results === []) {
44  return null;
45  }
46  if ($languageFieldName === null || $languageIds === null) {
47  return $results[0];
48  }
49  usort(
50  $results,
51  // orders records by there occurrence in $languageFallbackIds
52  function (array $a, array $b) use ($languageFieldName, $languageIds): int {
53  $languageA = (int)$a[$languageFieldName];
54  $languageB = (int)$b[$languageFieldName];
55  return array_search($languageA, $languageIds, true)
56  - array_search($languageB, $languageIds, true);
57  }
58  );
59  return $results[0];
60  }
61 
73  protected function resolveAllRelevantLanguageIds()
74  {
75  $languageIds = [-1, $this->siteLanguage->getLanguageId()];
76  foreach ($this->getLanguageAspect()->getFallbackChain() as $item) {
77  if (in_array($item, $languageIds, true) || !‪MathUtility::canBeInterpretedAsInteger($item)) {
78  continue;
79  }
80  $languageIds[] = (int)$item;
81  }
82  return $languageIds;
83  }
84 
92  protected function getLanguageAspect(): ‪LanguageAspect
93  {
94  if ($this->languageAspect === null) {
95  $this->languageAspect = ‪LanguageAspectFactory::createFromSiteLanguage($this->siteLanguage);
96  }
97  return $this->languageAspect;
98  }
99 }
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:73
‪TYPO3\CMS\Core\Context\LanguageAspectFactory
Definition: LanguageAspectFactory.php:25
‪TYPO3\CMS\Core\Site\SiteLanguageAwareTrait
Definition: SiteLanguageAwareTrait.php:25
‪TYPO3\CMS\Core\Context\LanguageAspectFactory\createFromSiteLanguage
‪static LanguageAspect createFromSiteLanguage(SiteLanguage $language)
Definition: LanguageAspectFactory.php:102
‪TYPO3\CMS\Core\Routing\Aspect\SiteLanguageAccessorTrait
Definition: SiteLanguageAccessorTrait.php:25
‪TYPO3\CMS\Core\Routing\Aspect\SiteLanguageAccessorTrait\resolveLanguageFallback
‪array null resolveLanguageFallback(array $results, ?string $languageFieldName, ?array $languageIds)
Definition: SiteLanguageAccessorTrait.php:39
‪TYPO3\CMS\Core\Context\LanguageAspect
Definition: LanguageAspect.php:55
‪TYPO3\CMS\Core\Routing\Aspect
Definition: AspectFactory.php:4
‪TYPO3\CMS\Core\Routing\Aspect\SiteLanguageAccessorTrait\resolveAllRelevantLanguageIds
‪int[] resolveAllRelevantLanguageIds()
Definition: SiteLanguageAccessorTrait.php:71
‪TYPO3\CMS\Core\Routing\Aspect\SiteLanguageAccessorTrait\$languageAspect
‪LanguageAspect $languageAspect
Definition: SiteLanguageAccessorTrait.php:29
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:21
‪TYPO3\CMS\Core\Routing\Aspect\SiteLanguageAccessorTrait\getLanguageAspect
‪LanguageAspect getLanguageAspect()
Definition: SiteLanguageAccessorTrait.php:90