Using and dispatching events in extensions

PSR-14 events can be used to extend the TYPO3 Core or third-party extensions.

You can find a complete list of events provided by the TYPO3 Core in the following chapter: Event list.

Events provided by third-party extensions should be described in the extension's manual. You can also search for events by looking for classes that inject the Psr\EventDispatcher\EventDispatcherInterface.

Listen to an event

If you want to use an event provided by the Core or a third-party extension, create a PHP class with a method __invoke(SomeCoolEvent $event) that accepts an object of the event class as argument. It is possible to use another method name but you have to configure the name in the Configuration/Services.yaml or it is not found.

It is best practice to use a descriptive class name and to put it in the namespace \MyVendor\MyExtension\EventListener.

<?php

// EXT:my_extension/Classes/EventListener/Joh316PasswordInformer.php
declare(strict_types=1);

namespace MyVendor\MyExtension\EventListener;

use Psr\Log\LoggerInterface;
use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\FrontendLogin\Event\PasswordChangeEvent;

/**
 * The password 'joh316' was historically used as the default password for
 * the TYPO3 install tool.
 * Today this password is an unsecure choice as it is well-known, too short
 * and does not contain capital letters or special characters.
 */
#[AsEventListener]
final class Joh316PasswordInvalidator
{
    public function __construct(
        private readonly LoggerInterface $logger,
    ) {}

    public function __invoke(PasswordChangeEvent $event): void
    {
        if ($event->getRawPassword() === 'joh316') {
            $this->logger->warning(sprintf(
                'User %s uses the default password "joh316".',
                $event->getUser()['username'],
            ));
        }
    }
}
Copied!

Dispatch an event

You can dispatch events in your own extension's code to enable other extensions to extend your code. Events are the preferred method of making code in TYPO3 extensions extendable.

See Event Dispatcher, Quickstart on how to create a custom event and dispatch it.