Provide hook names constants for Hook Attribute aguments

Created on 31 July 2025, 3 days ago

Problem/Motivation

After implementing support for Hook Attributes in the changelog https://www.drupal.org/node/3442349 β†’ - now it's much more convenient to manage hooks!

But the exact hook name, description, and the module which provides it, still remains a mystery from the code side ;)

Proposed resolution

Would be great to provide constants with all the hook names from Drupal Core, so attributes like this:

  #[Hook('views_data_alter')]

can become like this:

  #[Hook(Views::HOOK_VIEWS_DATA_ALTER)]

that will automatically give direct relationship, description, autocomplete, typo check, and all other benefits of using constants. What do you think about this idea?

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

✨ Feature request
Status

Active

Version

11.0 πŸ”₯

Component

base system

Created by

πŸ‡¦πŸ‡²Armenia murz Yerevan, Armenia

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

Comments & Activities

  • Issue created by @murz
  • πŸ‡¦πŸ‡²Armenia murz Yerevan, Armenia
  • πŸ‡ΊπŸ‡ΈUnited States nicxvan

    That's a lot of constants, many ides should already have auto completion.

    Maybe there is another way to do this?

  • πŸ‡¦πŸ‡²Armenia murz Yerevan, Armenia

    many ides should already have auto completion.

    Hmm, interesting, where then do IDEs get the information about those hook names to autocomplete? Just gathered them somehow and hardcoded them in the IDE plugin configuration? Okay, we can parse the Drupal Core code to gather all the hook names, but not additional hooks from contrib modules...

  • πŸ‡ΊπŸ‡ΈUnited States nicxvan

    From the *.api.php files, if contrib has documented them then it should work there too.

  • πŸ‡ΊπŸ‡ΈUnited States moshe weitzman Boston, MA

    Totally agree with this. I am not worried about a couple hundred constant definitions in a large codebase. *.api.php files are a Drupalism that should be on its way out IMO.

  • πŸ‡¬πŸ‡§United Kingdom catch

    Maybe this would be OK inside attributes, but we had serious problems with event constants in the past with modules being installed/uninstalled and should not repeat that πŸ› Event class constants for event names can cause fatal errors when a module is installed Active .

  • πŸ‡ΊπŸ‡ΈUnited States moshe weitzman Boston, MA

    Ugh. When an Attribute is read (via reflection), it throws a fatal when the constant is undefined This is why we cant have nice things.

  • πŸ‡ΊπŸ‡ΈUnited States nicxvan

    Yeah, we ran into that same issue with the order parameter.

    If we get rid of the *.api.php files where would we document the hooks?

  • πŸ‡ΊπŸ‡ΈUnited States moshe weitzman Boston, MA

    Answering the question with a question: Where do we document events that modules can subscribe to? Service tags that modules can implement? Plugins that modules can implement?

Production build 0.71.5 2024