‪TYPO3CMS  10.4
SiteLanguageAccessorTrait.php
Go to the documentation of this file.
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 
19 
24 
26 {
28 
32  protected $languageAspect;
33 
42  protected function resolveLanguageFallback(array $results, ?string $languageFieldName, ?array $languageIds): ?array
43  {
44  if ($results === []) {
45  return null;
46  }
47  if ($languageFieldName === null || $languageIds === null) {
48  return $results[0];
49  }
50  usort(
51  $results,
52  // orders records by there occurrence in $languageFallbackIds
53  function (array $a, array $b) use ($languageFieldName, $languageIds): int {
54  $languageA = (int)$a[$languageFieldName];
55  $languageB = (int)$b[$languageFieldName];
56  return array_search($languageA, $languageIds, true)
57  - array_search($languageB, $languageIds, true);
58  }
59  );
60  return $results[0];
61  }
62 
74  protected function resolveAllRelevantLanguageIds()
75  {
76  $languageIds = [-1, $this->siteLanguage->getLanguageId()];
77  foreach ($this->getLanguageAspect()->getFallbackChain() as $item) {
78  if (in_array($item, $languageIds, true) || !‪MathUtility::canBeInterpretedAsInteger($item)) {
79  continue;
80  }
81  $languageIds[] = (int)$item;
82  }
83  return $languageIds;
84  }
85 
93  protected function getLanguageAspect(): ‪LanguageAspect
94  {
95  if ($this->languageAspect === null) {
96  $this->languageAspect = ‪LanguageAspectFactory::createFromSiteLanguage($this->siteLanguage);
97  }
98  return $this->languageAspect;
99  }
100 }
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Core\Context\LanguageAspectFactory
Definition: LanguageAspectFactory.php:27
‪TYPO3\CMS\Core\Site\SiteLanguageAwareTrait
Definition: SiteLanguageAwareTrait.php:28
‪TYPO3\CMS\Core\Context\LanguageAspectFactory\createFromSiteLanguage
‪static LanguageAspect createFromSiteLanguage(SiteLanguage $language)
Definition: LanguageAspectFactory.php:34
‪TYPO3\CMS\Core\Routing\Aspect\SiteLanguageAccessorTrait
Definition: SiteLanguageAccessorTrait.php:26
‪TYPO3\CMS\Core\Routing\Aspect\SiteLanguageAccessorTrait\resolveLanguageFallback
‪array null resolveLanguageFallback(array $results, ?string $languageFieldName, ?array $languageIds)
Definition: SiteLanguageAccessorTrait.php:40
‪TYPO3\CMS\Core\Context\LanguageAspect
Definition: LanguageAspect.php:57
‪TYPO3\CMS\Core\Routing\Aspect
Definition: AspectFactory.php:18
‪TYPO3\CMS\Core\Routing\Aspect\SiteLanguageAccessorTrait\resolveAllRelevantLanguageIds
‪int[] resolveAllRelevantLanguageIds()
Definition: SiteLanguageAccessorTrait.php:72
‪TYPO3\CMS\Core\Routing\Aspect\SiteLanguageAccessorTrait\$languageAspect
‪LanguageAspect $languageAspect
Definition: SiteLanguageAccessorTrait.php:30
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\Routing\Aspect\SiteLanguageAccessorTrait\getLanguageAspect
‪LanguageAspect getLanguageAspect()
Definition: SiteLanguageAccessorTrait.php:91