Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Respect language fallbacks when resolving Posts #134

Merged
merged 2 commits into from
Mar 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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