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.

Example

Registration of the event listener in the extension's Services.yaml:

EXT:my_extension/Configuration/Services.yaml
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'
Copied!

Read how to configure dependency injection in extensions.

The corresponding event listener class:

EXT:my_extension/Classes/Redirects/EventListener/MyEventListener.php
<?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);
    }
}
Copied!

API

class ModifyAutoCreateRedirectRecordBeforePersistingEvent
Fully qualified name
\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 ( )
Returns
\TYPO3\CMS\Redirects\RedirectUpdate\SlugRedirectChangeItem
getSource ( )
Returns
\TYPO3\CMS\Redirects\RedirectUpdate\RedirectSourceInterface
getRedirectRecord ( )
Returns
array
setRedirectRecord ( array $redirectRecord)
param $redirectRecord

the redirectRecord