SlugRedirectChangeItemCreatedEvent¶
New in version 12.2.
The PSR-14 event \TYPO3\CMS\Redirects\Event\SlugRedirectChangeItemCreatedEvent
is fired in the \TYPO3\CMS\Redirects\RedirectUpdate\SlugRedirectChangeItemFactory
class and allows extensions to manage the redirect sources for which redirects
should be created.
TYPO3 already implements the
EXT:redirects/Classes/EventListener/AddPlainSlugReplacementSource.php
listener. It is used to add the plain slug value based source type, which
provides the same behavior as before. Implementing this as a Core listener
gives extension authors the ability to remove the source added by
AddPlainSlugReplacementSource
when their listeners are registered and
executed afterwards. See the example below.
The implementation of the EXT:redirects/Classes/RedirectUpdate/RedirectSourceInterface.php interface is required for custom source classes. Using this interface enables automatic detection of implementations. Additionally, this allows to transport custom information and data.
Example¶
Registration of the event listener in the extension's Services.yaml
:
MyVendor\MyExtension\Redirects\EventListener\MyEventListener:
tags:
- name: event.listener
identifier: 'my-extension/redirects/add-redirect-source'
after: 'redirects-add-plain-slug-replacement-source'
The corresponding event listener class:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Redirects\EventListener;
use MyVendor\MyExtension\Redirects\CustomSource;
use TYPO3\CMS\Redirects\Event\SlugRedirectChangeItemCreatedEvent;
use TYPO3\CMS\Redirects\RedirectUpdate\PlainSlugReplacementRedirectSource;
use TYPO3\CMS\Redirects\RedirectUpdate\RedirectSourceCollection;
final class MyEventListener
{
public function __invoke(SlugRedirectChangeItemCreatedEvent $event): void
{
// Retrieve change item and sources
$changeItem = $event->getSlugRedirectChangeItem();
$sources = $changeItem->getSourcesCollection()->all();
// Remove plain slug replacement redirect source from sources
$sources = array_filter(
$sources,
fn ($source) => !($source instanceof PlainSlugReplacementRedirectSource)
);
// Add custom source implementation
$sources[] = new CustomSource();
// Replace sources collection
$changeItem = $changeItem->withSourcesCollection(
new RedirectSourceCollection(...array_values($sources))
);
// Update changeItem in the event
$event->setSlugRedirectChangeItem($changeItem);
}
}
Example of a CustomSource
implementation:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Redirects;
use TYPO3\CMS\Redirects\RedirectUpdate\RedirectSourceInterface;
final class CustomSource implements RedirectSourceInterface
{
public function getHost(): string
{
return '*';
}
public function getPath(): string
{
return '/some-path';
}
public function getTargetLinkParameters(): array
{
return [];
}
}
API¶
- class TYPO3\CMS\Redirects\Event\SlugRedirectChangeItemCreatedEvent¶
This event is fired in the TYPO3CMSRedirectsRedirectUpdateSlugRedirectChangeItemFactory factory if a new SlugRedirectChangeItem is created.
It can be used to add additional sources, remove sources or completely remove the change item itself. A source must implement the RedirectSourceInterface, and for each source a redirect record is created later in the SlugService. If the SlugRedirectChangeItem is set to null, no further action is executed for this slug change.
- getSlugRedirectChangeItem()¶
- Return type
TYPO3\CMS\Redirects\RedirectUpdate\SlugRedirectChangeItem
- setSlugRedirectChangeItem(TYPO3\\CMS\\Redirects\\RedirectUpdate\\SlugRedirectChangeItem $slugRedirectChangeItem)¶
- Parameters
$slugRedirectChangeItem (
TYPO3\CMS\Redirects\RedirectUpdate\SlugRedirectChangeItem
) -- the slugRedirectChangeItem