Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 293630

Rethink #[Hook] attribute inheritance

$
0
0

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.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet


Viewing all articles
Browse latest Browse all 293630

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>