Problem/Motivation
State what you believe is wrong or missing from the current standards.
We should add guidelines for hook naming: https://project.pages.drupalcode.org/coding_standards/php/coding/#naming...
Benefits
If we adopted this change, the Drupal Project would benefit by more clarity in how to name a new hook.
Three supporters required
- https://www.drupal.org/u/{userid} (yyyy-mm-dd they added support)
- https://www.drupal.org/u/{userid} (yyyy-mm-dd they added support)
- https://www.drupal.org/u/{userid} (yyyy-mm-dd they added support)
Proposed changes
Create an MR with all proposed changes.
2. Repeat the above for each page or sub-page that needs to be changed.
Remaining tasks
Create this issue in the Coding Standards queue, using the defined template- Add supporters
- Create a Change Record
- Review by the Coding Standards Committee
- Coding Standards Committee takes action as required
- Discussed at a Core Committee meeting, if it impacts Drupal Core
- Final review by Coding Standards Committee
- Documentation updates
- Commit the MR
- Publish change record
- Remove 'Needs documentation edits' tag
- If applicable, create follow-up issues for PHPCS rules/sniffs changes
For a full explanation of these steps see the Coding Standards project page
Comments
Comment #2
borisson_What text would you add do that page? Can we create a merge request? I think this makes sense to change, having hooks that start with hook should probably be reserved only for hook altering hooks. Would this change break anything in drupal core currently?
Comment #3
quietone commentedHow does this fit with the existing section in core.api.php about naming a hook?
Comment #4
borisson_I think #3 is wrong, it should NOT start with `hook_` but instead with the module short name?
Comment #5
drunken monkeyWell, the dummy function you write for documenting the hook does start with
hook_and you then also call ithook_something()when referring to it in doc comments etc. It’s just that the machine name of the hook then does not include thishook_prefix. Maybe that’s where the confusion comes from? It’s really not very consistent.For instance:
I node.api.php:
In some other module’s
Hookservice:So, is the hook’s name
hook_node_grantsornode_grants? Completely open to interpretation, I’d say.I think the current rule is already what we want: The “machine name” of the hook, i.e., the string that goes inside
#[Hook('…')], should always start with the defining module’s short name. And for documenting the hook, the current standard is to prefix that machine name with the prefixhook_and define a function by that name, so all hooks can be found by searching for functions starting withhook_(orhook_MODULE_for a specific module). When referring to the hook in comments, we also use that prefixed name so that you can quickly get to the definition.We maybe just need to explain this distinction, and thereby the current standard, more clearly? What we of course do not want is for the string inside
#[Hook('…')]to start withhook_for any hooks not defined by Core.If we add a coding standard for this, especially one enforced with Coder (etc.), it will definitely need an exemption for Core.
Comment #6
nicxvan commentedThat is not accurate, only the hook name goes inside of the attribute like this
#[Hook('node_grants')]you can use the module parameter if you want to implement it on behalf of another module, but that is discouraged.There are no hooks in core that start with hook. hook_hook_info was the last one and that has been removed in Drupal 12. It also was never supported by OOP hook attributes so it would not affect this rule.
When looking at hook_node_grants. The hook name is
node_grants.hookonly signified that you should replace that in a procedural function with the module machine name.The documentation you see in *.api.php e.g. node.api.php is just documentation, those are not affected by the coder rule. It is still the established practice for documenting which hooks your module provides.
Comment #7
quietone commented@nicxvan, are you referring to a specific coder rule?
Can someone make an MR to discuss?
Comment #8
nicxvan commentedI was referring to this: #3499291: Add rule to check Hook attribute doesn't start with hook_
Comment #9
quietone commentedThanks,, then core needs an issue to implement that sniff.