Skip to content

Commit

Permalink
[FEATURE] Respect language fallbacks when resolving Posts (#134)
Browse files Browse the repository at this point in the history
Co-authored-by: Markus Klösges <[email protected]>
Co-authored-by: Benjamin Kott <[email protected]>
  • Loading branch information
3 people authored Mar 6, 2020
1 parent 418d97d commit 4a40a31
Showing 1 changed file with 53 additions and 4 deletions.
57 changes: 53 additions & 4 deletions Classes/Domain/Repository/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 4a40a31

Please sign in to comment.