AfterLinkIsGeneratedEvent
Note
It is recommended to use the PSR-14 event instead of the global
getATagParams hook (
$GLOBALS)
to add additional attributes (see example below) to links.
The PSR-14 event
\TYPO3\
allows PHP developers to modify any kind of link generated by TYPO3's mighty
typolink functionality.
By using this event, it is possible to add attributes to links to internal pages, or links to files, as the event contains the actual information of the link type with it.
As this event works with the
\TYPO3\
object it is possible to modify or replace the LinkResult information instead of
working with string replacement functionality for adding, changing or removing
attributes.
If a link could not be generated, a
\TYPO3\ might be thrown.
Example
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Frontend\EventListener;
use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Frontend\Event\AfterLinkIsGeneratedEvent;
#[AsEventListener(
identifier: 'my-extension/link-modifier',
)]
final readonly class MyEventListener
{
public function __invoke(AfterLinkIsGeneratedEvent $event): void
{
$linkResult = $event->getLinkResult()->withAttribute(
'data-enable-lightbox',
'true',
);
$event->setLinkResult($linkResult);
}
}
API
- class AfterLinkIsGeneratedEvent
-
- Fully qualified name
-
\TYPO3\
CMS\ Frontend\ Event\ After Link Is Generated Event
Generic event to modify any kind of link generation with typolink(). This is processed by all frontend-related links.
If a link could not be generated, a "UnableToLinkException" could be thrown by an Event Listener.