Attention
TYPO3 v11 has reached end-of-life as of October 31th 2024 and is no longer being maintained. Use the version switcher on the top left of this page to select documentation for a supported version of TYPO3.
Need more time before upgrading? You can purchase Extended Long Term Support (ELTS) for TYPO3 v11 here: TYPO3 ELTS.
ModifyHrefLangTagsEvent
New in version 10.3
Event to alter the hreflang tags just before they get rendered.
The class \TYPO3\
has been
refactored to be a listener (identifier 'typo3-seo/hreflangGenerator') to the
newly introduced event. This way the system extension seo still provides
hreflang tags but it is now possible to register after or instead
of the implementation.
Example
An example implementation could look like this:
EXT:
services:
Vendor\MyExtension\HrefLang\EventListener\OwnHrefLang:
tags:
- name: event.listener
identifier: 'my-ext/ownHrefLang'
after: 'typo3-seo/hreflangGenerator'
event: TYPO3\CMS\Frontend\Event\ModifyHrefLangTagsEvent
With after
and before
, you can make sure your own listener is
executed after or before the given identifiers.
EXT:
namespace Vendor\MyExtension\HrefLang\EventListener;
use TYPO3\CMS\Frontend\Event\ModifyHrefLangTagsEvent;
final class OwnHrefLang
{
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'
]
- param array $hrefLangs
-
the hrefLangs
- addHrefLang ( string $languageCode, string $url)
-
Add a hreflang tag to the current list of hreflang tags
- param string $languageCode
-
The language of the hreflang tag you would like to add. For example: nl-NL
- param string $url
-
The URL of the translation. For example: https://example.com/nl