Use something other than a single underscore for hook namespacing

Created on 13 August 2009, over 15 years ago
Updated 18 October 2024, 6 months ago

Problem/Motivation

The underscore separator in the hook naming is very brittle. If a module invents the foo_bar hook and another invents the bar hook and you also have two modules called something_foo and something then all hell breaks lose. Also, it's impossible to see from a function name whether it's a hook implementation or not (and unless every non-hook implementation function name is carefully prefixed with an underscore, just with enabling another module suddenly it might become one).

Update: Recently proposed alternatives

To avoid curly bracket namespace declarations, recent proposals suggest
Drupal\my_module\foo() (regular function)
Drupal\my_module\hook_foo() (hook implementation)
instead of what follows below.
See also comment #138 and #1428592: Proposal: PHP 5.3 Namespaces for module-provided functions and hook implementations. .

Proposed resolution

Right now hooks are defined as hook_foo where <code>foo is the name of the hook and hook_ is the module name. This would change to hooks/MODULENAME/foo where everything is defined in the hooks/MODULENAME namespace. For example:

function node_menu() { /* body of node_menu */ }
function node_help() { /* body of node_help */ }

becomes

namespace Drupal\hooks\node {
  function menu() { /* body of node_menu */ }
  function help() { /* body of node_help */ }
}

Now there is no way to mix up what function belongs to what module. It is immediately visible what's a hook implementation and what is not. We still can easy implement hooks on behalf of other modules because unlike classes, you can add to a namespace in several files.

If we are doing 📌 Move some rebuilds outside of Drupal Postponed: needs info then autoloading hook implementations becomes possible too.

Also, we can namespace hooks too:

namespace hooks\user\views {
  function data() {
    echo "user implements hook_views_data\n";
  }
}

and the hook is called views\data.

Remaining tasks

Convert every hook implementation in core. Note that we can make this an additional invoker on top of the current one: in addition to $function = $module . '_' . $hook; $function(); we also do $function = 'Drupal\\hooks\\' . $module . '\\' . $hook;$function(); and when the conversion is over just remove the older underscore based invoker.

User interface changes

None.

API changes

See above.

Original report by Damien Tournoud

This has been discussed numerous times. It's a good idea for countless reasons (see #451152: Implementing a hook on the behalf of another module and #367355: Module names should not contain an underscore for some of them).

📌 Task
Status

Closed: duplicate

Version

11.0 🔥

Component

base system

Created by

🇫🇷France Damien Tournoud

Live updates comments and jobs are added and updated live.
  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

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.71.5 2024