From 4a40a317f9b415512aaabc69136e484dcbda5ee6 Mon Sep 17 00:00:00 2001 From: MK-42 Date: Fri, 6 Mar 2020 17:28:59 +0100 Subject: [PATCH] [FEATURE] Respect language fallbacks when resolving Posts (#134) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Markus Klösges Co-authored-by: Benjamin Kott --- Classes/Domain/Repository/PostRepository.php | 57 ++++++++++++++++++-- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/Classes/Domain/Repository/PostRepository.php b/Classes/Domain/Repository/PostRepository.php index e07ca1a4..d3405dd0 100644 --- a/Classes/Domain/Repository/PostRepository.php +++ b/Classes/Domain/Repository/PostRepository.php @@ -10,12 +10,15 @@ namespace T3G\AgencyPack\Blog\Domain\Repository; +use Psr\Http\Message\ServerRequestInterface; use T3G\AgencyPack\Blog\Constants; use T3G\AgencyPack\Blog\Domain\Model\Author; use T3G\AgencyPack\Blog\Domain\Model\Category; use T3G\AgencyPack\Blog\Domain\Model\Post; use T3G\AgencyPack\Blog\Domain\Model\Tag; use TYPO3\CMS\Core\Context\Context; +use TYPO3\CMS\Core\Site\Entity\Site; +use TYPO3\CMS\Core\Site\Entity\SiteLanguage; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\RootlineUtility; use TYPO3\CMS\Extbase\Configuration\ConfigurationManager; @@ -234,21 +237,67 @@ public function findCurrentPost(): ?Post $pageId = $typoScriptFrontendController ? (int)$typoScriptFrontendController->id : (int)GeneralUtility::_GP('id'); + + $currentLanguageId = (int)GeneralUtility::makeInstance(Context::class) + ->getPropertyFromAspect('language', 'id', 0); + + $post = $this->getPostWithLanguage($pageId, $currentLanguageId); + + if ($post !== null) { + return $post; + } + + return $this->applyLanguageFallback($pageId, $currentLanguageId); + } + + protected function getPostWithLanguage(int $pageId, int $languageId): ?Post + { $query = $this->createQuery(); $constraints = $this->defaultConstraints; - if ((int)GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('language', 'id', 0) > 0) { + + if ($languageId > 0) { $constraints[] = $query->equals('l10n_parent', $pageId); + $constraints[] = $query->equals('sys_language_uid', $languageId); } else { $constraints[] = $query->equals('uid', $pageId); } - /** @var Post $post */ - $post = $query + return $query ->matching($query->logicalAnd($constraints)) ->execute() ->getFirst(); + } + + /** + * @param int $pageId the uid of the page for which fallback languages should be resolved + * @param int $currentLanguageId the requested language, for which fallback languages should be resolved + * @return Post|null + */ + protected function applyLanguageFallback(int $pageId, int $currentLanguageId): ?Post + { + $currentSite = $this->getCurrentSite(); + if ($currentSite) { + /** @var SiteLanguage $languageConfiguration */ + $languageConfiguration = $currentSite->getAllLanguages()[$currentLanguageId]; + // check the whole language-fallback chain + $fallbacks = $languageConfiguration->getFallbackLanguageIds(); + foreach ($fallbacks as $fallbackLanguageId) { + $post = $this->getPostWithLanguage($pageId, $fallbackLanguageId); + if ($post !== null) { + return $post; + } + } + } + return null; + } - return $post; + protected function getCurrentSite(): ?Site + { + if ($GLOBALS['TYPO3_REQUEST'] instanceof ServerRequestInterface + && $GLOBALS['TYPO3_REQUEST']->getAttribute('site') instanceof Site) { + return $GLOBALS['TYPO3_REQUEST']->getAttribute('site'); + } + return null; } /**