Skip to content

Commit

Permalink
Implemented output of rel="canonical"
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar committed Oct 26, 2021
1 parent e737edf commit a02c18f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
1 change: 1 addition & 0 deletions core-bundle/src/Resources/config/services.yml
Expand Up @@ -702,6 +702,7 @@ services:
- '@Contao\CoreBundle\Routing\ResponseContext\ResponseContextAccessor'
- '@event_dispatcher'
- '@contao.security.token_checker'
- '@contao.framework'
public: true

contao.search.indexer.default:
Expand Down
10 changes: 8 additions & 2 deletions core-bundle/src/Resources/contao/pages/PageRegular.php
Expand Up @@ -13,7 +13,6 @@
use Contao\CoreBundle\Exception\NoLayoutSpecifiedException;
use Contao\CoreBundle\Routing\ResponseContext\CoreResponseContextFactory;
use Contao\CoreBundle\Routing\ResponseContext\HtmlHeadBag\HtmlHeadBag;
use Contao\CoreBundle\Routing\ResponseContext\JsonLd\ContaoPageSchema;
use Contao\CoreBundle\Routing\ResponseContext\JsonLd\JsonLdManager;
use Contao\CoreBundle\Routing\ResponseContext\ResponseContext;
use Contao\CoreBundle\Routing\ResponseContext\ResponseContextAccessor;
Expand Down Expand Up @@ -80,7 +79,8 @@ protected function prepare($objPage)
$locale = LocaleUtil::formatAsLocale($objPage->language);

$container = System::getContainer();
$container->get('request_stack')->getCurrentRequest()->setLocale($locale);
$request = $container->get('request_stack')->getCurrentRequest();
$request->setLocale($locale);
$container->get('translator')->setLocale($locale);

$this->responseContext = $container->get(CoreResponseContextFactory::class)->createContaoWebpageResponseContext($objPage);
Expand Down Expand Up @@ -224,6 +224,12 @@ protected function prepare($objPage)
// Meta robots tag
$this->Template->robots = $this->responseContext->get(HtmlHeadBag::class)->getMetaRobots();

// Canonical
if ($objPage->enableCanonical)
{
$this->Template->canonical = $this->responseContext->get(HtmlHeadBag::class)->getCanonicalUri($request);
}

// Fall back to the default title tag
if (!$objLayout->titleTag)
{
Expand Down
Expand Up @@ -13,6 +13,12 @@
<meta name="generator" content="Contao Open Source CMS">
<?php $this->endblock(); ?>

<?php $this->block('canonical'); ?>
<?php if ($this->canonical): ?>
<link rel="canonical" href="<?= $this->canonical; ?>">
<?php endif; ?>
<?php $this->endblock(); ?>

<?= $this->viewport ?>
<?= $this->framework ?>
<?= $this->stylesheets ?>
Expand Down
Expand Up @@ -12,10 +12,13 @@

namespace Contao\CoreBundle\Routing\ResponseContext;

use Contao\Controller;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\Routing\ResponseContext\HtmlHeadBag\HtmlHeadBag;
use Contao\CoreBundle\Routing\ResponseContext\JsonLd\ContaoPageSchema;
use Contao\CoreBundle\Routing\ResponseContext\JsonLd\JsonLdManager;
use Contao\CoreBundle\Security\Authentication\Token\TokenChecker;
use Contao\Environment;
use Contao\PageModel;
use Contao\StringUtil;
use Spatie\SchemaOrg\WebPage;
Expand All @@ -26,12 +29,14 @@ class CoreResponseContextFactory
private ResponseContextAccessor $responseContextAccessor;
private EventDispatcherInterface $eventDispatcher;
private TokenChecker $tokenChecker;
private ContaoFramework $contaoFramework;

public function __construct(ResponseContextAccessor $responseContextAccessor, EventDispatcherInterface $eventDispatcher, TokenChecker $tokenChecker)
public function __construct(ResponseContextAccessor $responseContextAccessor, EventDispatcherInterface $eventDispatcher, TokenChecker $tokenChecker, ContaoFramework $contaoFramework)
{
$this->responseContextAccessor = $responseContextAccessor;
$this->eventDispatcher = $eventDispatcher;
$this->tokenChecker = $tokenChecker;
$this->contaoFramework = $contaoFramework;
}

public function createResponseContext(): ResponseContext
Expand Down Expand Up @@ -83,6 +88,20 @@ public function createContaoWebpageResponseContext(PageModel $pageModel): Respon
$htmlHeadBag->setMetaRobots($pageModel->robots);
}

if ($pageModel->enableCanonical && $pageModel->canonicalLink) {
$url = $this->contaoFramework->getAdapter(Controller::class)->replaceInsertTags($pageModel->canonicalLink, false);

// Ensure absolute links (FIXME: Remove once we remove support for relative urls)
if (!preg_match('#^https?://#', $url)) {
$url = $this->contaoFramework->getAdapter(Environment::class)->get('base').$url;
}
$htmlHeadBag->setCanonicalUri($url);
}

if ($pageModel->enableCanonical && $pageModel->canonicalKeepParams) {
$htmlHeadBag->setKeepParamsForCanonical(array_map('trim', explode(',', (string) $pageModel->canonicalKeepParams)));
}

$jsonLdManager
->getGraphForSchema(JsonLdManager::SCHEMA_CONTAO)
->set(
Expand Down

0 comments on commit a02c18f

Please sign in to comment.