- 🇬🇧United Kingdom catch
Now a duplicate of 📌 Hux-style hooks, proof of concept Needs work !
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).
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. →
.
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
.
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.
None.
See above.
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).
Closed: duplicate
11.0 🔥
base system
Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.
Not all content is available!
It's likely this issue predates Contrib.social: some issue and comment data are missing.
Now a duplicate of 📌 Hux-style hooks, proof of concept Needs work !