Project

General

Profile

Bug #88545 » LanguageFallbackCanonicalGenerator.php

./Classes/Canonical/LanguageFallbackCanonicalGenerator.php - Benjamin Serfhos, 2020-05-04 10:46

 
<?php

namespace Serfhos\MyExample\Canonical;

use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\LanguageAspect;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3\CMS\Frontend\Utility\CanonicalizationUtility;

class LanguageFallbackCanonicalGenerator
{
/** @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController */
private $typoScriptFrontendController;

/** @var \TYPO3\CMS\Core\Context\LanguageAspect */
private $languageAspect;

/**
* CanonicalGenerator constructor
*
* @param \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController|null $typoScriptFrontendController
* @param \TYPO3\CMS\Core\Context\LanguageAspect|null $languageAspect
*/
public function __construct(
TypoScriptFrontendController $typoScriptFrontendController = null,
LanguageAspect $languageAspect = null
) {
$this->typoScriptFrontendController = $typoScriptFrontendController ?? $this->getTypoScriptFrontendController();
$this->languageAspect = $languageAspect ?? GeneralUtility::makeInstance(Context::class)->getAspect('language');
}

/**
* @param string $href
* @return array
*/
public function checkForActiveLanguageFallback(string &$href): array
{
$languageId = $this->languageAspect->getId();
if ($languageId > 0 && $this->languageAspect->getContentId() !== $languageId) {
$href = $this->typoScriptFrontendController->cObj->typoLink_URL([
'parameter' => $this->typoScriptFrontendController->id . ',' . $this->typoScriptFrontendController->type,
'forceAbsoluteUrl' => true,
'additionalParams' => '&L=' . $this->languageAspect->getContentId(),
'addQueryString' => true,
'addQueryString.' => [
'method' => 'GET',
'exclude' => implode(
',',
CanonicalizationUtility::getParamsToExcludeForCanonicalizedUrl(
(int)$this->typoScriptFrontendController->id,
(array)$GLOBALS['TYPO3_CONF_VARS']['FE']['additionalCanonicalizedUrlParameters']
)
),
],
]);
}

return [$href];
}

/**
* @return TypoScriptFrontendController
*/
protected function getTypoScriptFrontendController(): TypoScriptFrontendController
{
return $GLOBALS['TSFE'];
}
}
(2-2/2)