Events

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.

Note

This example is outdated as the used event PasswordChangeEvent does not provide the method setAsInvalid() anymore. However, it illustrates the basic principle of an event listener.

<?php

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

namespace MyVendor\MyExtension\EventListener;

use TYPO3\CMS\FrontendLogin\Event\PasswordChangeEvent;

/**
 * The password 'joh316' was historically used as 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.
 */
final class Joh316PasswordInvalidator
{
    public function __invoke(PasswordChangeEvent $event): void
    {
        if ($event->getRawPassword() === 'joh316') {
            $event->setAsInvalid('This password is not allowed');
        }
    }
}

Then register the event in your extension's Configuration/Services.yaml:

# EXT:my_extension/Configuration/Services.yaml
services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false
  # ...
  MyVendor\MyExtension\EventListener\Joh316PasswordInvalidator:
    tags:
      - name: event.listener
        identifier: 'myJoh316PasswordInvalidator'

Additionally, the Configuration/Services.yaml file allows to define a different method name for the event listener class and to influence the order in which events are loaded. See Registering the event listener via Services.yaml for details.

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.