AfterLinkIsGeneratedEvent¶
New in version 12.0: This PSR-14 event supersedes the UrlProcessorInterface
logic
which allowed to modify mail URNs or external URLs, but not the
full anchor tag.
In addition, this PSR-14 event also replaces the
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_content.php']['typoLink_PostProc']
hook which was not executed at all times, and had a cumbersome API
to modify values.
It is also recommended to use the PSR-14 event instead of the global
getATagParams hook ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_content.php']['getATagParamsPostProc']
)
to add additional attributes (see example below) to links.
The PSR-14 event \TYPO3\CMS\Frontend\Event\AfterLinkIsGeneratedEvent
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\CMS\Frontend\Typolink\LinkResultInterface
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\CMS\Frontend\Typolink\UnableToLinkException
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 class MyEventListener
{
public function __invoke(AfterLinkIsGeneratedEvent $event): void
{
$linkResult = $event->getLinkResult()->withAttribute(
'data-enable-lightbox',
'true'
);
$event->setLinkResult($linkResult);
}
}
New in version 13.0: The PHP attribute \TYPO3\CMS\Core\Attribute\AsEventListener
has been
introduced to tag a PHP class as an event listener. Alternatively, or if you
need to be compatible with older TYPO3 versions, you can also register an
event listener via the Configuration/Services.yaml
file. Switch to
an older version of this page for an example or have a look at the section
Implementing an event listener in your extension.
API¶
- class TYPO3\CMS\Frontend\Event\AfterLinkIsGeneratedEvent¶
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.
- setLinkResult(TYPO3\\CMS\\Frontend\\Typolink\\LinkResultInterface $linkResult)¶
Update a link when a part was modified by an Event Listener.
- Parameters
$linkResult (
TYPO3\CMS\Frontend\Typolink\LinkResultInterface
) -- the linkResult
- getLinkResult()¶
- Return type
TYPO3\CMS\Frontend\Typolink\LinkResultInterface
- getContentObjectRenderer()¶
- Return type
TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
- getLinkInstructions()¶
Returns the original instructions / $linkConfiguration that were used to build the link
- Return type
array