ModifyHrefLangTagsEvent
The PSR-14 event
\TYPO3\
is available to alter
the hreflang
tags just before they get rendered.
The class \TYPO3\
(identifier
typo3-
) is also available as an event. Its purpose
is to provide the default hreflang
tags. This way it is possible to
register a custom event listener after or instead of this implementation.
Example
An example implementation could look like this:
services:
# Place here the default dependency injection configuration
MyVendor\MyExtension\Frontend\EventListener\MyEventListener:
tags:
- name: event.listener
identifier: 'my-extension/cache-timeout'
after: 'typo3-seo/hreflangGenerator'
Read how to configure dependency injection in extensions.
With after
and before
, you can make sure your own listener is
executed after or before the given identifiers.
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Frontend\EventListener;
use TYPO3\CMS\Frontend\Event\ModifyHrefLangTagsEvent;
final class MyEventListener
{
public function __invoke(ModifyHrefLangTagsEvent $event): void
{
$hrefLangs = $event->getHrefLangs();
$request = $event->getRequest();
// Do anything you want with $hrefLangs
$hrefLangs = [
'en-US' => 'https://example.org',
'nl-NL' => 'https://example.org/nl',
];
// Override all hrefLang tags
$event->setHrefLangs($hrefLangs);
// Or add a single hrefLang tag
$event->addHrefLang('de-DE', 'https://example.org/de');
}
}
API
- class ModifyHrefLangTagsEvent
-
- Fully qualified name
-
\TYPO3\
CMS\ Frontend\ Event\ Modify Href Lang Tags Event
Listeners to this event will be able to modify the hreflang tags that will be generated. You can use this when you have an edge case language scenario and need to alter the default hreflang tags.
- setHrefLangs ( array $hrefLangs)
-
Set the hreflangs. This should be an array in format:
[ 'en-US' => 'https://example.com', 'nl-NL' => 'https://example.com/nl' ]
Copied!- param $hrefLangs
-
the hrefLangs
- addHrefLang ( string $languageCode, string $url)
-
Add a hreflang tag to the current list of hreflang tags
- param $languageCode
-
The language of the hreflang tag you would like to add. For example: nl-NL
- param $url
-
The URL of the translation. For example: https://example.com/nl