Problem/Motivation
When implementing a hook following the hook_foo_BAR_ID_bar()
pattern, the following formats are allowed (at least for D7):
* Implements hook_foo_BAR_ID_bar() for xyz_bar().
* Implements hook_foo_BAR_ID_bar() for xyz-bar.html.twig.
* Implements hook_foo_BAR_ID_bar() for xyz-bar.tpl.php.
For some hooks I think this can look misleading as in the following examples:
-
/** * Implements hook_ENTITY_TYPE_load() for node(). */
-
/** * Implements hook_feeds_PLUGIN_TYPE_config_defaults() for parser(). */
The enforcement on ending the item reference with parentheses, implies that the referenced item is a function, which in the above two examples it is not.
Background
While working on improving the coding standards for the D7 version of Feeds, I got warned about a coding standards violation for the following piece of code:
/**
* Implements hook_features_pipe_COMPONENT_alter() for 'feeds_importer'.
*
* Automatically adds dependencies when a Feed importer is selected in Features.
*/
See https://www.drupal.org/pift-ci-job/824119
Proposed resolution
I therefore propose to also allow this format:
* Implements hook_foo_BAR_ID_bar() for 'xyz_bar'.
Its use however MUST be limited to items that are not a function nor a template.
Existing issues
I've been looking for existing issues to find out if there maybe is a specific reason for why the proposed format would not be allowed, but I couldn't find one so far. What I did find:
- First time hook_foo_BAR_ID_bar() format got allowed:
#1346754: False positives on certain hook implementations. - Error message got corrected:
#1419752: Error with unknown @see reference and hook_FORM_ID_alter - Allowing template files to be referenced:
#1805544: Modify sniff for "Implements hook_foo_BAR_ID_bar() for xyz_bar()" - Support for Twig templates:
#2203627: "Implements hook_foo() for some-template.file" sniff doesn't support Twig template names - Theme hooks should reference templates:
#2660774: Hook implementation formatting issues
Format is already used in D8 core
I saw that D8 core already uses this format and also similar formats.
- From field.module, Drupal 8.4.2:
/** * Implements hook_ENTITY_TYPE_update() for 'field_storage_config'. * * Reset the field handler settings, when the storage target_type is changed on * an entity reference field. */
- From block.module, Drupal 8.4.2:
/** * Implements hook_ENTITY_TYPE_delete() for 'configurable_language'. * * Delete the potential block visibility settings of the deleted language. */
Other formats used in core:
- From block.module, Drupal 8.4.2:
/** * Implements hook_ENTITY_TYPE_delete() for menu entities. */
- From node.module, Drupal 8.4.2:
/** * Implements hook_ENTITY_TYPE_predelete() for user entities. */
Comments
Comment #2
jhodgdon+1.
Comment #3
drunken monkey+1
I personally prefer using double quotes in comments, but otherwise I agree, this should definitely be allowed!
+1 for allowing both single and double quotes around the variable part instead of call parantheses. (Since the Coder rule is apparently disabled by default currently, I didn't even know this wasn't allowed already.)
We should probably also make this clearer in the documentation. There, we don't even list the (explicitly allowed) variants for template files.
Comment #4
Dave ReidAnother format to consider is "Implements hook_NAME() on behalf of MODULENAME.module." This can be seen often used in contributed modules to implement their module's hooks on behalf of core modules (since core cannot implement contributed module hooks).
Comment #5
MegaChriz CreditAttribution: MegaChriz as a volunteer commented@Dave Reid
That's is a good one. The D7 version of Feeds implements some hooks on behalf of core modules too. Now that is just documented as
Implements hook_NAME()
, with the core module documented in the@file
docs:Comment #6
Dave ReidHrm, I assume that wouldn't be valid since a file shouldn't have multiple @file docblocks?
From https://www.drupal.org/docs/develop/standards/api-documentation-and-comm...
Comment #7
jhodgdonYeah there can only be one @file per file. But that example might be a whole file of stuff Feeds implements for Core locale?