Feature: #102835 - Add PSR-14 events to manipulate TypoLinkCodecService
See forge#102835
Description
TYPO3's main API for encoding and decoding TypoLink's has been extended
and now provides two new PSR-14 events \TYPO3\
and \TYPO3\
, which
allow developers to fully manipulate the encoding and decoding functionality.
A common use case for extensions is to extend the TypoLink parts to allow
editors adding additional information, e.g. custom attributes to be added
to the link markup. Previously, this required extensions to extended / cross
class Typo
. This is no longer necessary when using the
new events.
The Before
therefore allows to set $parameters
,
to be encoded while the After
allows to modify the
decoded $typo
.
Both events provide the used $delimiter
and the $empty
next to the corresponding input value, either the $typo
to be
encoded or the $typo
to be decoded.
Example
The event listener class, using the PHP attribute #
for
registration:
use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Core\LinkHandling\Event\AfterTypoLinkDecodedEvent;
use TYPO3\CMS\Core\LinkHandling\Event\BeforeTypoLinkEncodedEvent;
final class TypoLinkCodecServiceEventListener
{
#[AsEventListener]
public function encodeTypoLink(BeforeTypoLinkEncodedEvent $event): void
{
$typoLinkParameters = $event->getParameters();
if (str_contains($typoLinkParameters['class'] ?? '', 'foo')) {
$typoLinkParameters['class'] .= ' bar';
$event->setParameters($typoLinkParameters);
}
}
#[AsEventListener]
public function decodeTypoLink(AfterTypoLinkDecodedEvent $event): void
{
$typoLink = $event->getTypoLink();
$typoLinkParts = $event->getTypoLinkParts();
if (str_contains($typoLink, 'foo')) {
$typoLinkParts['foo'] = 'bar';
$event->setTypoLinkParts($typoLinkParts);
}
}
}
Impact
Using the new PSR-14 events, it's now possible to fully influence the encoding and decoding of any TypoLink.