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

  1. https://www.drupal.org/u/{userid} (yyyy-mm-dd they added support)
  2. https://www.drupal.org/u/{userid} (yyyy-mm-dd they added support)
  3. 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

  1. Create this issue in the Coding Standards queue, using the defined template
  2. Add supporters
  3. Create a Change Record
  4. Review by the Coding Standards Committee
  5. Coding Standards Committee takes action as required
  6. Discussed at a Core Committee meeting, if it impacts Drupal Core
  7. Final review by Coding Standards Committee
  8. Documentation updates
    1. Commit the MR
    2. Publish change record
    3. Remove 'Needs documentation edits' tag
  9. 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

nicxvan created an issue. See original summary.

borisson_’s picture

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?

quietone’s picture

How does this fit with the existing section in core.api.php about naming a hook?

 * @section defining Defining a hook
 *
 * To define a hook:
 * - Choose a unique name for your hook. It should start with "hook_", followed
 *   by your module's short name.
 * - Provide documentation in a *.api.php file in your module's main
 *   directory. See the "implementing" section above for details of what this
 *   should contain (parameters, return value, and sample function body).
 * - Invoke the hook in your module's code.
borisson_’s picture

Title: Add guideline that hooks should not begin with the work hook_ » Add guideline that hooks should not begin with the word hook_

I think #3 is wrong, it should NOT start with `hook_` but instead with the module short name?

drunken monkey’s picture

Well, the dummy function you write for documenting the hook does start with hook_ and you then also call it hook_something() when referring to it in doc comments etc. It’s just that the machine name of the hook then does not include this hook_ prefix. Maybe that’s where the confusion comes from? It’s really not very consistent.

For instance:

I node.api.php:


/**
 * Inform the node access system what permissions the user has.
 *
 * …
 */
function hook_node_grants(AccountInterface $account, $operation): array {
  // …
}

In some other module’s Hook service:

  /**
   * Implements hook_node_grants().
   */
  #[Hook('node_grants')]
  public function nodeGrants($account, $operation): array {
    // …
  }

So, is the hook’s name hook_node_grants or node_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 prefix hook_ and define a function by that name, so all hooks can be found by searching for functions starting with hook_ (or hook_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 with hook_ 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.

nicxvan’s picture

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.

That 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.

#[Hook('…')] to start with hook_ for any hooks not defined by Core.

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. hook only 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.

quietone’s picture

The documentation you see in *.api.php e.g. node.api.php is just documentation, those are not affected by the coder rule. I

@nicxvan, are you referring to a specific coder rule?

Can someone make an MR to discuss?

nicxvan’s picture

quietone’s picture

Thanks,, then core needs an issue to implement that sniff.