Stop using magic functions and force modules to declare the hooks they want to implement

Created on 28 March 2011, about 13 years ago
Updated 8 March 2023, over 1 year ago

What are the magic functions buying us? In my experience, developers are confused when they start trying to figure out where what is getting called by whom. Plus, we have lots of expensive aparatus for discovering these functions and maintaining reliable lists of them. We don't respond well to state changes (like modules moving to a new directory, or two versions of the same module in the code base. And it's also a relatively inflexible structure. We can't, for instance, declare that I want hook_x to run, but I want it to ran after some other module runs its hook_x (okay we can with crazy alter functions, but it is pretty intense.

This pattern would follow a more typical observer pattern, similar to the one that is used in jQuery with events:

function mymodule_init($drupal) {
$drupal->tellMeAbout('menu', 'mymodule_menu');
// could even...
$drupal->runABeforeB('menu', 'someothermodule','mymodule');
}

Modules would remain the same, they would simply need to add some kind of function to the top of the module file which every module would have to declare. It would be an easy thing to script with coder.

This would solve a lot of our problems IMO, and help DX a lot while only forcing module maintainers to add a very small amount of code. If others are interested in what I admit is a somewhat radical departure of the land of magic functions, I will draft up a patch. I don't think it would take too long.

As an aside, other modules could use this pattern since we are using dependency injection with the event registry / controller thing being passed in. So the views module could let its submodules / includes register hooks that are specifically only for views.

✨ Feature request
Status

Closed: duplicate

Version

9.5

Component
Base  →

Last updated 1 minute ago

Created by

🇮🇳India JacobSingh

Live updates comments and jobs are added and updated live.
  • API clean-up

    Refactors an existing API or subsystem for consistency, performance, modularization, flexibility, third-party integration, etc. May imply an API change. Frequently used during the Code Slush phase of the release cycle.

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.

Production build 0.69.0 2024