ModifyAutoCreateRedirectRecordBeforePersistingEvent
New in version 12.3
The PSR-14 event
\TYPO3\
allows extensions to modify the redirect record before it is persisted to
the database. This can be used to change values according to circumstances, such
as different sub-tree settings that are not covered by the Core
site configuration. Another use case could be to write data to additional
sys_
columns added by a custom extension for later use.
Note
To handle updates or react on manual created redirects in the backend
module, available hooks of \TYPO3\
can be used.
Example
Registration of the event listener in the extension's Services.
:
services:
# Place here the default dependency injection configuration
MyVendor\MyExtension\Redirects\EventListener\MyEventListener:
tags:
- name: event.listener
identifier: 'my-extension/modify-auto-create-redirect-record-before-persisting'
Read how to configure dependency injection in extensions.
The corresponding event listener class:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Redirects\EventListener;
use TYPO3\CMS\Redirects\Event\ModifyAutoCreateRedirectRecordBeforePersistingEvent;
use TYPO3\CMS\Redirects\RedirectUpdate\PlainSlugReplacementRedirectSource;
final class MyEventListener
{
public function __invoke(
ModifyAutoCreateRedirectRecordBeforePersistingEvent $event,
): void {
// Only work on plain slug replacement redirect sources.
if (!($event->getSource() instanceof PlainSlugReplacementRedirectSource)) {
return;
}
// Get prepared redirect record and change some values
$record = $event->getRedirectRecord();
// Override the status code, eventually to another value than
// configured in the site configuration
$record['status_code'] = 307;
// Set value to a field extended by a custom extension, to persist
// additional data to the redirect record.
$record['custom_field_added_by_a_extension']
= 'page_' . $event->getSlugRedirectChangeItem()->getPageId();
// Update changed record in event to ensure changed values are saved.
$event->setRedirectRecord($record);
}
}
API
- class ModifyAutoCreateRedirectRecordBeforePersistingEvent
-
- Fully qualified name
-
\TYPO3\
CMS\ Redirects\ Event\ Modify Auto Create Redirect Record Before Persisting Event
This event is fired in the TYPO3CMSRedirectsServiceSlugService before a redirect record is persisted for changed page slug.
It can be used to modify the redirect record before persisting it. This gives extension developers the ability to apply defaults or add custom values to the record.