Allow @param and @return docs on hook implementations

Created on 22 May 2019, almost 6 years ago
Updated 13 September 2023, over 1 year ago

From https://www.drupal.org/docs/develop/standards/api-documentation-and-comm...

Hook implementation
Documentation uses a short format:

/**
 * Implements hook_help().
 */
function blog_help($section) {

If appropriate, you can add additional details pertinent to the particular hook implementation in a separate paragraph after the summary. Do not document parameters and return value though -- these are documented on the hook definition referenced in the summary line.

I think this is bad advice, because

  • IDEs do not look at the hook definition for type hints (I am only speaking for the IDE I use myself).
  • In many cases (in D7 at least), the hook definition in old contrib modules is missing proper type hints on the @param and @return docs. So even if the IDE would try, it would find nothing.
  • As a human reading code, I don't want to jump to the hook definition all the time.

It should be possible for a contrib or custom module to add the @param and @return docs on a hook implementation, to get full support from the IDE and to clarify things that are missing in the hook definition doc.

I already do this in projects I maintain myself, but I don't want to be arguing with my team members or a client about it who (rightfully) point out what is currently the standard in Drupal.

It could be made explicit that this only applies to custom or contrib code and is optional.

Feature request
Status

Active

Component

Coding Standards

Created by

🇩🇪Germany donquixote

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇬🇧United Kingdom Alina Basarabeanu

    I need help with the hook implementation coding standards as I can't find a way to fix it
    Example:

    /**
     * Implements hook_page_attachments().
     */
    function test_page_attachments(array &$page): void {

    Error retund by the validation:
    Function test_page_attachments() does not have @param annotation for its traversable parameter $page.

    If I add the @param I get a different error:
    Hook implementations should not duplicate @param documentation

    How can I fix it?

  • Status changed to Needs work over 1 year ago
  • 🇩🇪Germany DiDebru

    Same Issue for me as mentioned in #5.
    Also -1 for me maintain annotations is quite a lot of work.

  • Status changed to Active over 1 year ago
  • 🇳🇴Norway steinmb

    Feature requests are "never" critical.

  • 🇮🇳India chetan 11

    /**
    * Implements hook_preprocess_HOOK() for page templates.
    *
    * Prepares variables for page templates.
    *
    * @param array &$variables
    * An associative array of variables for the page template.
    * - node: The node object, if there is an active node associated with the page.
    * - logged_in: A flag indicating whether the current user is logged in.
    * @param string $hook
    * The name of the theme hook being invoked. Typically "page".
    *
    * @see template_preprocess_page()
    */
    function mymodule_preprocess_page(&$variables, $hook) {
    // Implementation code here.
    }

    /**
    * Implements hook_menu().
    *
    * Defines menu items and page callbacks.
    *
    * @return array
    * An associative array describing menu items and their page callbacks.
    *
    * @see mymodule_menu()
    */
    function mymodule_menu() {
    $items = array();

    $items['example-page'] = array(
    'title' => 'Example Page',
    'page callback' => 'mymodule_example_page',
    'access callback' => 'user_access',
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
    );

    return $items;
    }

    /**
    * Page callback for the example page.
    *
    * @param array $args
    * An array of arguments passed from the URL path.
    *
    * @return string
    * The rendered content for the page.
    */
    function mymodule_example_page($args) {
    // Implementation code here.
    return 'Hello, this is the example page content.';
    }

  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    Using PhpStorm, I get messages from the IDE for hooks. I still think it is a bad idea, to add @param and @return tags to hook implementations.
    Except in few cases where parameters can have a type that is more specific, in most of the cases, the parameter's types are the same for all the hook implementations. It also seems a feature that goes in the opposite direction taken when the documentation comment for class constructors has been done not mandatory.

  • 🇳🇿New Zealand quietone

    The Coding Standards Committee has developed a custom issue template to assist in managing issues efficiently. We have already found that it's use is helping. For issues created before the template was in use I am adding the new template to the Issue Summary and asking everyone here to help convert to it..

    Thank you for your help!

  • Status changed to Closed: works as designed 2 months ago
  • 🇳🇿New Zealand quietone

    Reading this issue, I do not see any support for this proposal, which has been open for 6 years. A year ago I updated the issue summary with the new template and that did not provoke a response. So, I think it is time to close this.

    If you support this idea, re-open the issue and add a comment.

Production build 0.71.5 2024