Problem/Motivation

For new module developers hooks are a bit confusing to figure out how they work. One helpful bit is when the hooks show _FORM_ID_ or THEME_ the identify the parts of the hook that need to be changed for your module or theme.

Proposed resolution

Id propose making all the hook_ to HOOK_ to promote this visual modifiable part of the identifier throughout core.

Before:
hook_entity_type_alter
After:
HOOK_entity_type_alter

Remaining tasks

Discuss any drawbacks.
Create a meta and add to coder review.

User interface changes

n/a

API changes

n/a

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Jon Pugh’s picture

FileSize
136.24 KB

+1 to that! Makes it much more consistent.

Attached is a quick search and replace.

Jon Pugh’s picture

Status: Active » Needs review
joelpittet’s picture

That's awesome @Jon Pugh! Is there any implements hook_... to change as well?

Jon Pugh’s picture

FileSize
799.26 KB

Yep, a few ;)

Did a final replace of all instances of hook_, there are many @see and other mentions in comments.

Left any instances of `_hook_`, as those functions shouldn't be changed.

So for example:

function HOOK_hook_info() {
Jon Pugh’s picture

This is a big patch so I made a Pull Request on my mirror on GitHub, in case anyone wants to review there:

https://github.com/jonpugh/drupal/pull/1/files

joelpittet’s picture

Oh wow, that is a lot, and is green too! Thanks a lot again @Jon Pugh.

Hope to get some people and especially @jhodgdon's thoughts on this change?

jhodgdon’s picture

Status: Needs review » Needs work

My opinion is NO PLEASE NO.

Why?

a) We've been doing it the other way for as far as I know, the entire history of Drupal.

b) The Hooks topic on api.drupal.org already clearly explains how to implement a hook.
https://api.drupal.org/api/drupal/core!includes!module.inc/group/hooks/8

c) There are also a ***lot*** of drupal.org docs pages explaining this, and many pages that will have to be updated if all the hook function names change, not to mention many books on Drupal programming.

d) A lot of links to api.drupal.org will be wrong because the hook names changed.

e) Pages on api.drupal.org for the D7 and D6 hooks will now not show links to the corresponding D8 hooks, because the names changed.

So I think this should be closed as "works as designed".

joelpittet’s picture

Thanks for the details @jhodgdon on what would have to change for this to get in and the gut reaction.

In response to the whys?
a) Because it has done like this, doesn't mean it should remain like this... obviously. And there is inconsistencies with THEME, FORM_ID, MENUNAME, etc and other uppercased replaceable tokens within the hook system. The issue that triggered this issue was I noticed some fixes for THEME and MENUNAME in #1533104: Make bartik theme pass Coder Review and wondered why it wasn't consistent accross core.

b) No where in that document does it explain that you should replace HOOK and no reason given why FOO is replaced and not BAR. So that is more of a case and point rather than an example of good documentation of how hooks work.

c) Adding documentation to explain the documentation doesn't help the issue. Maybe there should be less docs and more concise and intuitive instead of having to over explain the "how and why" of HOOKs?

d) Thanks for bringing this up, I would have never though of that. I've seen slight name changes show up in the API docs before maybe this could be automated away?

e) Same response as D.

jhodgdon’s picture

One more, definitely serious objection:

f) If you want to talk about consistency, then for consistency with other places we use all caps, like _FORM_ID_ or THEME_, you would need to use MODULE_, not HOOK_, because it is being replaced by the module name, not a "hook" name.

In fact, we are already using _HOOK_ to mean "the theme hook" and _MODULE_ to mean "the module short name" in:
https://api.drupal.org/api/drupal/includes!theme.inc/function/theme/7
which is on a topic in D8:
https://api.drupal.org/api/drupal/core!modules!system!theme.api.php/grou...

So you would need to change, for instance, hook_help() to be called MODULE_help(), if you want to maintain this consistency and actually give module developers something obvious to know what to replace.

Which means the hooks wouldn't even be called hook_something at all.

alexpott’s picture

Version: 8.x-dev » 9.x-dev

My initial gut on reading the issue title was "hmm, makes sense". But, as @jhodgdon outlines this change affects far more than just code and the consistent thing would be to replace hook_ with something else - MODULE_, THEME_, EXTENSION_ are possibilities. But given where we in the release cycle and where effort needs to be focussed I think we should discuss this again for d9.

catch’s picture

Title: Rename hook_ to HOOK_ to identify the part of the function name needing change » Rename hook_ to MODULE_ or EXTENSION_ to identify the part of the function name needing change
Version: 9.x-dev » 8.1.x-dev

Since this is documentation I think we could make it optional in a minor version and deprecate hook_ - api.drupal.org would need to support both until 9.x.

Whether we want to actually make the change is a different matter, but moving to 8.1.x since it's not impossible to do there.

jhodgdon’s picture

Just as a note, api.d.o will need to support both hook_ and whatever is eventually decided on here, for the forseeable future. The reason is that api.d.o does not know about coding standards changes; its code must be able to display pages for Drupal 7, 6, 5, 8, 9, etc. with the same logic. That said, changing it to support MODULE_ or EXTENSION_ as well has hook_ will not be difficult. There's just a small bit of logic that says "If this function name starts with hook_ then try to find implementations by pattern matching on the rest of the name". That would just need a slight bit more to say "starts with hook_ or ...". Just a few lines of code would change.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.