Problem/Motivation
The #[Hook] attribute is used to mark a method as a hook implementations.
In #3495943: Handle module preprocess functions as OOP hooks and #3479141: Implement FormAlter attribute we introduced subclasses FormAlter extends Hook and Preprocess extends Hook.
This also changed the main Hook class:
- The class constants PREFIX and SUFFIX were introduced, with the intention to override them.
- The $hook parameter became optional, at least in the public signature.
At the same time, people who look up the constructor signature of Preprocess or FormAlter will only find the parent constructor, where the main parameter is called $hook. For theme and alter hooks, this is not correct: The parameter should ideally be called $theme_hook or $alter_type, respectively, to distinguish from the parameter given to Hook.
Steps to reproduce
Proposed resolution
Drop the constants, and override the constructor directly.
If needed, introduce a base class instead of extending an existing attribute (not really needed for now, but as general best practice)
This is also how e.g. symfony attributes extend Autowire.
Overriding a constructor allows to provided dedicated documentation, and also allows to fine-tune the signature to the specific use case.