ModifyUrlForCanonicalTagEvent

With the PSR-14 event \TYPO3\CMS\Seo\Event\ModifyUrlForCanonicalTagEvent the URL for the href attribute of the canonical tag can be altered or emptied.

New in version 12.4.9: The methods getRequest() and getPage() have been added.

Example

Registration of the event in the Services.yaml:

EXT:my_extension/Configuration/Services.yaml
services:
  # Place here the default dependency injection configuration

  MyVendor\MyExtension\Seo\EventListener\MyEventListener:
    tags:
      - name: event.listener
        identifier: 'my-extension/modify-url-for-canonical-tag'

Changing the host of the current request and setting it as canonical:

EXT:my_extension/Classes/Seo/EventListener/MyEventListener.php
<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\Seo\EventListener;

use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Seo\Event\ModifyUrlForCanonicalTagEvent;

final class MyEventListener
{
    public function __invoke(ModifyUrlForCanonicalTagEvent $event): void
    {
        // Note: $event->getUrl() is dispatched with the empty string value ''
        $currentUrl = $this->getRequest()->getUri();
        $newCanonical = $currentUrl->withHost('example.com');
        $event->setUrl((string)$newCanonical);
    }

    private function getRequest(): ServerRequestInterface
    {
        return $GLOBALS['TYPO3_REQUEST'];
    }
}

API

class TYPO3\CMS\Seo\Event\ModifyUrlForCanonicalTagEvent

PSR-14 event to alter (or empty) a canonical URL for the href="" attribute of a canonical URL.

getUrl()
Return type

string

setUrl(string $url)
Parameters
  • $url (string) -- the url

getRequest()
Return type

Psr\Http\Message\ServerRequestInterface

getPage()
Return type

TYPO3\CMS\Core\Domain\Page