ModifyAutoCreateRedirectRecordBeforePersistingEvent¶
New in version 12.3.
The PSR-14 event
\TYPO3\CMS\Redirects\Event\ModifyAutoCreateRedirectRecordBeforePersistingEvent
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_redirect
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\CMS\Core\DataHandling\DataHandler
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 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\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\Redirects\Event\ModifyAutoCreateRedirectRecordBeforePersistingEvent¶
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.
- getSlugRedirectChangeItem()¶
- Return type
TYPO3\CMS\Redirects\RedirectUpdate\SlugRedirectChangeItem
- getSource()¶
- Return type
TYPO3\CMS\Redirects\RedirectUpdate\RedirectSourceInterface
- getRedirectRecord()¶
- Return type
array
- setRedirectRecord(array $redirectRecord)¶
- Parameters
$redirectRecord (
array
) -- the redirectRecord