ModifyUrlForCanonicalTagEvent 

With the PSR-14 event TYPO3CMSSeoEventModifyUrlForCanonicalTagEvent the URL for the href attribute of the canonical tag can be altered or emptied.

This event is being dispatched after the standard functionality has been executed, such as fetching the URL from the page properties. Effectively, this also means that getUrl() might already return a non-empty string.

Example 

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 TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Seo\Event\ModifyUrlForCanonicalTagEvent;
use TYPO3\CMS\Seo\Exception\CanonicalGenerationDisabledException;

#[AsEventListener(
    identifier: 'my-extension/modify-url-for-canonical-tag',
)]
final readonly class MyEventListener
{
    public function __invoke(ModifyUrlForCanonicalTagEvent $event): void
    {
        if ($event->getCanonicalGenerationDisabledException() instanceof CanonicalGenerationDisabledException) {
            return;
        }

        // Only set the canonical in our example when the tag is not disabled
        // via TypoScript or via "no_index" in the page properties.
        $currentUrl = $event->getRequest()->getUri();
        $newCanonical = $currentUrl->withHost('example.com');
        $event->setUrl((string)$newCanonical);
    }
}
Copied!

API 

class ModifyUrlForCanonicalTagEvent
Fully qualified name
\TYPO3\CMS\Seo\Event\ModifyUrlForCanonicalTagEvent

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

getUrl ( )
Returns
string
setUrl ( string $url)
param $url

the url

getRequest ( )
Returns
PsrHttpMessageServerRequestInterface
getPage ( )
Returns
TYPO3CMSCoreDomainPage
getCanonicalGenerationDisabledException ( )
Returns
?TYPO3CMSSeoExceptionCanonicalGenerationDisabledException