Event class constants for event names can cause fatal errors when a module is installed

Created on 7 November 2016, over 7 years ago
Updated 15 July 2023, 11 months ago

Problem/Motivation

Hooks are invoked and events dispatched using an arbitrary string. Hook implementations and event listeners then get picked up based on that arbitrary string.

However, with events, we've adopted the pattern of putting the string in a constant on an Events class. See for example https://api.drupal.org/api/drupal/core!modules!migrate!src!Event!Migrate...

This has caused problems on module enable, see #2712647: Update Symfony components to ~3.2 β†’ and #2776235: Cached autoloader misses cause failures when missed class becomes available β†’ .

Event listeners reference the event class in getSubscribedEvents() - but they either have to rely on the event class always being loaded, or use class_exists() to check if it's available. This introduces a very fragile dependency on the classloader being updated when the module list is.

Proposed resolution

Stop using event classes with constants for event names, just use strings everywhere.

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component
ExtensionΒ  β†’

Last updated 3 days ago

No maintainer
Created by

πŸ‡¬πŸ‡§United Kingdom catch

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡·πŸ‡΄Romania claudiu.cristea Arad πŸ‡·πŸ‡΄

    And $classEvent::EVENT_1, $classEvent::EVENT_2 when there is more than one thing using the event (though the original pattern might have been there for discoverability) OR just create a base class and use ::class anyway.

    I think we should avoid using an event class for many events. Each event should have its own class, even that would need a base class or a trait to handle core reusability

Production build 0.69.0 2024