This module is intended for the developers and allows them to handle Drupal hooks in the event subscriber provided by the Symfony framework.
All of the hooks in Drupal core that are invoked by the module handler can be now registered in the subscriber by using the hook name.

Similar modules

  1. Hook Event Dispatcher - this module does a great job, is well supported and feature-rich. The downside is that in order to make it fully work with all hooks it has to implement each hook one by one and dispatch the event inside. On the other hand, it has a bunch of event-specific events.
  2. Hux - This very well written module is replacing the hooks by the OOP implementation but it is not using the event subscribers. If you are looking for something lightweight this might be a good choice.


How this module is different from the others?

It is decorating the core @module_handler and dispatching generic types of events depending on the case - the HookAlterEvent, HookInvokeEvent or HookPreprocesEvent.

It is scanning all the *.api.php files in order to find the hook definition along with the names of the parameter to make the interaction with the event object more friendly, ex. in the hook_entity_update, the entity object can by simply get by running:

$event->getArgument('entity');

in your event subscriber.

In case the hook does not have the implementation you can simply create the *.api.php file in your custom module and define the hook over there.
If you don't want to do that, the module will simply use the numerically keyed array so you can access the argument by its position:
$event->getArgument(0);

This module will also allow you to easily define custom event class by defining {your_module}.hook_events.yml file.

Road map:

  1. Guess object parameter name by the given type in case the hook definition will be missing ex. EntityInterface will be transformed into entity by taking the object class name or interface name (in this case cutting the interface suffix).
  2. Add dynamic arguments method names via __call for hooks with definition, ex. $event->getEntity(); in case the entity argument is defined.

Important notes:

  1. If the hook is invoked for the particular module (by ->invoke() method), the event won't be dispatched if the module has its hook implementation-defined.
  2. We are starting the development from Drupal 9.4 we are not going to maintain any lower Drupal version.


Project information

Releases