Feature: #97544 - PSR-14 events for modifying preview URIs
See forge#97544
Description
Two new PSR-14 events \TYPO3\
and \TYPO3\
have been introduced. Those serve as a direct replacement for the now deprecated
$GLOBALS
hook.
The Before
is executed in the
Preview
, before the preview URI is actually built.
It allows to either adjust the parameters, such as the page ID or the language ID,
or to set a custom preview URI, which will then stop the event propagation and
also prevents Preview
from building the URI based on the
parameters.
Methods of Before
:
set
Preview Uri (Uri Interface $uri) get
Page Id () set
Page Id (int $page Id) get
Language Id () set
Language Id (int $language Id) get
Rootline () set
Rootline (array $rootline) get
Section () set
Section (string $section) get
Additional Query Parameters () set
Additional Query Parameters (array $additional Query Parameters) get
Context () get
Options ()
Note
The overwritten parameters are used for building the URI and are also
passed to the After
. They however
do not overwrite the related class properties in Preview
.
The After
is executed in the
Preview
, after the preview URI has been built -
or set by an event listener to Before
. It
allows to overwrite the built preview URI. This event however does not feature
the possibility to modify the parameters, since this won't have any effect as
the preview URI is directly returned after event dispatching and no
further action is done by the Preview
.
Methods of After
:
set
Preview Uri (Uri Interface $uri) get
Preview Uri () get
Page Id () get
Language Id () get
Rootline () get
Section () get
Additional Query Parameters () get
Context () get
Options ()
Example
Registration of the event in your extension's Services.
:
MyVendor\MyPackage\Backend\MyEventListener:
tags:
- name: event.listener
identifier: 'my-package/backend/modify-parameters'
method: 'modifyParameters'
- name: event.listener
identifier: 'my-package/backend/modify-preview-uri'
method: 'modifyPreviewUri'
The corresponding event listener class:
use TYPO3\CMS\Backend\Routing\Event\AfterPagePreviewUriGeneratedEvent;
use TYPO3\CMS\Backend\Routing\Event\BeforePagePreviewUriGeneratedEvent;
final class MyEventListener
{
public function modifyParameters(BeforePagePreviewUriGeneratedEvent $event): void
{
// Add custom query parameter before URI generation
$event->setAdditionalQueryParameters(
array_replace_recursive(
$event->getAdditionalQueryParameters(),
['myParam' => 'paramValue']
)
);
}
public function modifyPreviewUri(AfterPagePreviewUriGeneratedEvent $event): void
{
// Add custom fragment to built preview URI
$uri = $event->getPreviewUri();
$uri = $uri->withFragment('#customFragment');
$event->setPreviewUri($uri);
}
}
Impact
It's now possible to modify the parameters used to build a preview URI and
also to directly set a custom preview URI, using the new PSR-14 event
Before
. It's also now possible to
modify or completely replace a built preview URI using the new PSR-14 event
After
.