‪TYPO3CMS  ‪main
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 
37  protected function resolveLanguageFallback(array $results, ?string $languageFieldName, ?array $languageIds): ?array
38  {
39  if ($results === []) {
40  return null;
41  }
42  if ($languageFieldName === null || $languageIds === null) {
43  return $results[0];
44  }
45  usort(
46  $results,
47  // orders records by there occurrence in $languageFallbackIds
48  static function (array $a, array $b) use ($languageFieldName, $languageIds): int {
49  $languageA = (int)$a[$languageFieldName];
50  $languageB = (int)$b[$languageFieldName];
51  return array_search($languageA, $languageIds, true)
52  - array_search($languageB, $languageIds, true);
53  }
54  );
55  return $results[0];
56  }
57 
69  protected function resolveAllRelevantLanguageIds()
70  {
71  $languageIds = [-1, $this->siteLanguage->getLanguageId()];
72  foreach ($this->getLanguageAspect()->getFallbackChain() as $item) {
73  if (in_array($item, $languageIds, true) || !‪MathUtility::canBeInterpretedAsInteger($item)) {
74  continue;
75  }
76  $languageIds[] = (int)$item;
77  }
78  return $languageIds;
79  }
80 
87  protected function getLanguageAspect(): ‪LanguageAspect
88  {
89  if ($this->languageAspect === null) {
90  $this->languageAspect = ‪LanguageAspectFactory::createFromSiteLanguage($this->siteLanguage);
91  }
92  return $this->languageAspect;
93  }
94 }
‪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 createFromSiteLanguage(SiteLanguage $language)
Definition: LanguageAspectFactory.php:31
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Core\Routing\Aspect\SiteLanguageAccessorTrait
Definition: SiteLanguageAccessorTrait.php:26
‪TYPO3\CMS\Core\Routing\Aspect\SiteLanguageAccessorTrait\resolveLanguageFallback
‪resolveLanguageFallback(array $results, ?string $languageFieldName, ?array $languageIds)
Definition: SiteLanguageAccessorTrait.php:35
‪TYPO3\CMS\Core\Context\LanguageAspect
Definition: LanguageAspect.php:57
‪TYPO3\CMS\Core\Routing\Aspect\SiteLanguageAccessorTrait\getLanguageAspect
‪getLanguageAspect()
Definition: SiteLanguageAccessorTrait.php:85
‪TYPO3\CMS\Core\Routing\Aspect
Definition: AspectFactory.php:18
‪TYPO3\CMS\Core\Routing\Aspect\SiteLanguageAccessorTrait\resolveAllRelevantLanguageIds
‪int[] resolveAllRelevantLanguageIds()
Definition: SiteLanguageAccessorTrait.php:67
‪TYPO3\CMS\Core\Routing\Aspect\SiteLanguageAccessorTrait\$languageAspect
‪LanguageAspect $languageAspect
Definition: SiteLanguageAccessorTrait.php:30
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24