Skip to content

Instantly share code, notes, and snippets.

@Schweriner
Created March 24, 2023 12:37
Show Gist options
  • Save Schweriner/a553de5770b13f93ec3f68fdfbed022e to your computer and use it in GitHub Desktop.
Save Schweriner/a553de5770b13f93ec3f68fdfbed022e to your computer and use it in GitHub Desktop.
TYPO3 Redirect to default page url if it is not translated
<?php
namespace PaulBeck\TueSitetemplate;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class SiteLanguageRedirect implements MiddlewareInterface
{
/**
* @var ServerRequestInterface
*/
protected $request = null;
/**
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
/** @var \TYPO3\CMS\Core\Http\Request request */
$this->request = $request;
/** @var \TYPO3\CMS\Core\Routing\PageArguments $routing */
$routing = $this->request->getAttributes()['routing'];
/** @var \TYPO3\CMS\Core\Site\Entity\SiteLanguage $language */
$languageId = $this->request->getAttributes()['language']->getLanguageId();
if($languageId === 0) {
return $handler->handle($request);
}
$pid = $routing->getPageId();
$qb = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)
->getQueryBuilderForTable('pages');
$translationExists = (bool) $qb->select('*')->from('pages')
->where(
$qb->expr()->eq('l10n_parent', $pid),
$qb->expr()->eq('sys_language_uid', $languageId)
)->execute()->rowCount();
if($translationExists === true) {
return $handler->handle($request);
}
$configuration = [
'parameter' => $pid,
'language' => 0,
'addQueryString' => 1,
'forceAbsoluteUrl' => true
];
$cObj = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
$uriToDefaultPage = $cObj->typoLink_URL($configuration);
return new \TYPO3\CMS\Core\Http\RedirectResponse($uriToDefaultPage, 302);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment