Change record status: 
Project: 
Introduced in branch: 
9.1.x
Introduced in version: 
9.1.0
Description: 

Drupal has been updated for upstream changes in the Symfony 5 Events system.

Event class changes

Symfony\Component\EventDispatcher\Event has been deprecated in Symfony 4, and Drupal will now trigger a deprecation notice when it is passed to Drupal\Component\EventDispatcher::dispatch().

There is a new Symfony\Contracts\EventDispatcher\Event class, however existing contributed event listeners may type hint on Symfony's deprecated event class, so updating directly to dispatch this event may result in fatal errors when not all contrib modules have been updated.

Instead, to enable both forward compatibility with Symfony 5, and backwards compatibility with existing Drupal 9 contributed modules, a new Drupal\Component\EventDispatcher\Event class has been added. This should be used instead of Symfony\Component\EventDispatcher\Event when dispatching events. In most cases this is as simple as changing the use statement:

Before:

use Symfony\Component\EventDispatcher\Event;

After:

use Drupal\Component\EventDispatcher\Event;

Other Drupal or Symfony Event subclasses may be used as without changes, since these will be updated to inherit from Symfony contracts as part of the Symfony 5 update in Drupal 10.

When an event listener is type hinting with the Symfony\Component\EventDispatcher\Event class in an event listener, either remove the type-hint altogether, or change the type hint to a more specific Event class if one is available.

EventDispatcher::dispatch() argument order

When dispatching events, Symfony has changed the argument order, so that the Event class is first, and the name is second.

Before:

$event_dispatcher->dispatch($event_name, $event);

After:

$event_dispatcher->dispatch($event, $event_name);

Drupal 9.1.x will trigger a deprecation notice when the old argument order is used.

Extending ContainerAwareEventDispatcher

In some cases, modules may be extending ContainerAwareEventDispatcher themselves and swapping the core service. In this case, there will be a hard break on Drupal 9.1.x due to the signature change to the ::dispatch() method. If you are only supporting Drupal 9.1.x, you can update to the new method signature (potentially copying the core changes). To support 8.9.x, keep your original class, create a new version that is compatible with 9.1.x, then when swapping the service, do so conditionally based on Symfony version.

Type hinting EventDispatcherInterface

Since Symfony 4.3, Symfony\Component\EventDispatcher\EventDispatcherInterface extends Symfony\Contracts\EventDispatcher\EventDispatcherInterface. All type hints can should be updated to point to the Symfony\Contracts version of the interface to allow classes extending both the new and deprecated interface to be passed.

Impacts: 
Module developers