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
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Redirects\EventListener;
use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Redirects\Event\ModifyAutoCreateRedirectRecordBeforePersistingEvent;
use TYPO3\CMS\Redirects\RedirectUpdate\PlainSlugReplacementRedirectSource;
#[AsEventListener(
identifier: 'my-extension/modify-auto-create-redirect-record-before-persisting',
)]
final readonly 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);
}
}
New in version 13.0
The PHP attribute \TYPO3\
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/
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 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.