API page: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21theme.api.php/function/hook_theme_suggestions_HOOK/8.2.x

In this page's documentation the following code example is given:

function hook_theme_suggestions_HOOK(array $variables) {
  $suggestions = array();

  $suggestions[] = 'node__' . $variables['elements']['#langcode'];

  return $suggestions;
}

I think it would be clearer if we used:

function hook_theme_suggestions_HOOK(array $variables) {
  $suggestions = array();

  $suggestions[] = 'hookname__'. $variables['elements']['#langcode'];

  return $suggestions;
}

In the original example the 'node' hook is used. For generality, 'hookname__' would be more appropriate.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ahmad.h.khalil created an issue. See original summary.

dawehner’s picture

This sounds like a good idea! Do you want to provide a patch?

WebKings.ca’s picture

I don't think we can patch a documentation page...

I tried looking for the "edit" tab on the API page documentation but it's not there. I think its because it's locked for a person with higher permission.

prajaankit’s picture

Assigned: Unassigned » prajaankit
prajaankit’s picture

Hi ahmad.h.khalil,

Patch will be the good solution, people see the example in core files first,
so we change in core api, it would be more helpful for the user.

prajaankit’s picture

Assigned: prajaankit » Unassigned
Status: Active » Needs review
WebKings.ca’s picture

Good to know @prajaankit...

I think with this change the code would be clearer...

prajaankit’s picture

Status: Needs review » Reviewed & tested by the community
Dinesh18’s picture

It is much more clearer using "hookname" instead of node.
Looks great. +1 to RTBC.

WebKings.ca’s picture

By the way @prajaankit I don't think a patch is the best solution since the first place people will look at is the actual API documentation on Drupal.org website. Anybody starting out to learn about Drupal 8 will look there first NOT in the core API files.

Secondly, if you look at this issue queue the component is "Documentation" not a core files issue. This should be fixed on the drupal.org documentation page

Read this link:
https://www.drupal.org/node/24565

Under "What to report in a Documentation issue"

It says:
"A page that you don't have permission to edit (request that the page be unlocked so that you can edit it to fix the problem)."

Also:

API documentation on api.drupal.org:
Use project "Drupal core" for issues related to the API documentation on api.drupal.org. The easiest way to create an issue is to click the "Create an issue" link on the api.drupal.org page that has the documentation problem. That will fill in most of the issue fields for you, and you'll just need to add a description of what the problem is to the title and body fields.

WebKings.ca’s picture

Status: Reviewed & tested by the community » Active

Documentation page on drupal.org still not changed. Required change has to be implemented.

WebKings.ca’s picture

Priority: Normal » Major
prajaankit’s picture

Hi @ahmad.h.khalil,

i Checked that how to change in api document, AS well as update in drupal core also, it would be helpful.

Thanks

prajaankit’s picture

Status: Active » Needs review

Thanks

WebKings.ca’s picture

Status: Needs review » Active

This issue is regarding the api.drupal.org documentation page NOT the core Drupal files. So the purpose of this issue is to fix the actual api.drupal.org as seen in the link provided in the initial issue description. To change the actual core files make another issue.

@prajaankit Do NOT change the status of this issue until it's resolved. OR Fix it yourself in the API Page at the following link:
https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21theme.api.php/function/hook_theme_suggestions_HOOK/8.2.x

Since the original issue in question is not resolved, I'm re-opening this issue.

cilefen’s picture

Priority: Major » Normal
Status: Active » Needs review

@ahmad.h.khalil Thank you for your interest in this issue. There are some things you should know:

  • api.drupal.org is generated from the code.
  • Because of that, changes are not effected until the code is committed.

I hope this helps!

cilefen’s picture

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

@prajaankit Thank you for providing a patch on this documentation bug. Please be aware that all reviews are peer-based so it is not permitted to mark one's own patches as "Reviewed and tested by the community". Overall, you've done a nice job—please continue helping on issues!

cilefen’s picture

Title: Code example should be modified to be more clearer » hook_theme_suggestions_HOOK() is not clear enough about what should be returned

One more thing: I have retitled this issue for clarity.

WebKings.ca’s picture

@cilefen I am updating the following link to indicate this vital piece of information. Thanks for the clarification.

https://www.drupal.org/node/24565

Dinesh18’s picture

Status: Needs review » Reviewed & tested by the community

Changing the status to RTBC.
Manually applied the patch as mentioned in #9

  • Gábor Hojtsy committed 75f627e on 8.3.x
    Issue #2883699 by prajaankit, ahmad.h.khalil, cilefen:...

  • Gábor Hojtsy committed c3f81d1 on 8.4.x
    Issue #2883699 by prajaankit, ahmad.h.khalil, cilefen:...
Gábor Hojtsy’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -docs infrastructure

I was wondering if hookname was the right thing or not, so did this:

$ git grep hookname
core/lib/Drupal/Core/Render/theme.api.php:    $suggestions[] = 'hookname__' . 'no_header';


$ git grep HOOKNAME
[ no results ]


$ git grep HOOK
core/includes/theme.inc: * hook_theme_suggestions_HOOK_alter() or adding to 'attributes' class key
core/includes/theme.inc: *   hook_theme_suggestions_HOOK_alter() or to $variables['attributes']['class']
core/lib/Drupal/Core/Entity/entity.api.php: * callback. Alternatively, it could also implement hook_preprocess_HOOK() for
core/lib/Drupal/Core/Entity/entity.api.php: * callback. Alternatively, it could also implement hook_preprocess_HOOK() for
core/lib/Drupal/Core/Render/theme.api.php: * called theme_HOOK, where HOOK is the name of the theme hook (for example,
core/lib/Drupal/Core/Render/theme.api.php: * defining a function called THEME_HOOK() in its THEME.theme file, where THEME
core/lib/Drupal/Core/Render/theme.api.php: * HOOK indicates the hook being called or a less specific hook. For example, if
core/lib/Drupal/Core/Render/theme.api.php: * - template_preprocess_HOOK(&$variables): Should be implemented by the module
core/lib/Drupal/Core/Render/theme.api.php: * - MODULE_preprocess_HOOK(&$variables): hook_preprocess_HOOK() is invoked on
core/lib/Drupal/Core/Render/theme.api.php: * - ENGINE_engine_preprocess_HOOK(&$variables): Allows the theme engine to set
core/lib/Drupal/Core/Render/theme.api.php: * - THEME_preprocess_HOOK(&$variables): Allows the theme to set necessary
core/lib/Drupal/Core/Render/theme.api.php: * preprocess functions (the ones ending in _HOOK) are called from the list
core/lib/Drupal/Core/Render/theme.api.php: * - A module can implement hook_theme_suggestions_HOOK(). This allows the
core/lib/Drupal/Core/Render/theme.api.php: * theme-hook-specific hook_theme_suggestions_HOOK_alter() and the generic
core/lib/Drupal/Core/Render/theme.api.php: * hooks implemented as functions. hook_preprocess_HOOK() can be used to
core/lib/Drupal/Core/Render/theme.api.php:function hook_preprocess_HOOK(&$variables) {
core/lib/Drupal/Core/Render/theme.api.php: * HOOK is the least-specific version of the hook being called. For example, if
core/lib/Drupal/Core/Render/theme.api.php: * @see hook_theme_suggestions_HOOK_alter()
core/lib/Drupal/Core/Render/theme.api.php:function hook_theme_suggestions_HOOK(array $variables) {
core/lib/Drupal/Core/Render/theme.api.php: * theme hook it's best to use hook_theme_suggestions_HOOK_alter().
core/lib/Drupal/Core/Render/theme.api.php: * hook_theme_suggestions_HOOK_alter(). So, for each module or theme, the more
core/lib/Drupal/Core/Render/theme.api.php: * @see hook_theme_suggestions_HOOK_alter()
core/lib/Drupal/Core/Render/theme.api.php: * hook_theme_suggestions_HOOK() or by earlier invocations of this hook.
core/lib/Drupal/Core/Render/theme.api.php: * HOOK is the least-specific version of the hook being called. For example, if
core/lib/Drupal/Core/Render/theme.api.php: * @see hook_theme_suggestions_HOOK()
core/lib/Drupal/Core/Render/theme.api.php:function hook_theme_suggestions_HOOK_alter(array &$suggestions, array $variables) {
core/lib/Drupal/Core/Render/theme.api.php: *     functions. If an implementation of hook_theme_suggestions_HOOK() (where
core/lib/Drupal/Core/Render/theme.api.php: *     HOOK is the base hook) changes the suggestion order, a different
core/lib/Drupal/Core/Render/theme.api.php: *     hook_theme_suggestions_HOOK() this suggestion remains the first
core/lib/Drupal/Core/Render/theme.api.php: *     for a module this will be filled in as template_preprocess_HOOK. For
core/lib/Drupal/Core/Render/theme.api.php: *     twig_preprocess_HOOK as well as themename_preprocess and
core/lib/Drupal/Core/Render/theme.api.php: *     themename_preprocess_HOOK.
core/lib/Drupal/Core/Theme/ThemeManager.php:    // Invoke hook_theme_suggestions_HOOK().
core/lib/Drupal/Core/Theme/ThemeManager.php:    // hook_theme_suggestions_HOOK_alter().
core/lib/Drupal/Core/Theme/ThemeManager.php:    // via hook_theme_suggestions_HOOK_alter(), enabling a theme to have
core/modules/aggregator/aggregator.module: * Implements hook_preprocess_HOOK() for block templates.
core/modules/block/block.api.php: * callback. Alternatively, it could also implement hook_preprocess_HOOK() for
core/modules/block/block.module: * Implements hook_theme_suggestions_HOOK().
core/modules/comment/comment.module: * Implements hook_preprocess_HOOK() for block templates.
core/modules/content_moderation/content_moderation.module: * Implements hook_preprocess_HOOK().
core/modules/content_moderation/src/ContentPreprocess.php:   * Wrapper for hook_preprocess_HOOK().
core/modules/content_translation/content_translation.admin.inc: * (proxied) Implements hook_preprocess_HOOK();
core/modules/content_translation/content_translation.module: * Implements hook_preprocess_HOOK() for language-content-settings-table.html.twig.
core/modules/filter/tests/src/Functional/FilterCaptionTwigDebugTest.php:    $this->assertTrue(strpos($output, '<!-- THEME HOOK: \'filter_caption\' -->') !== FALSE, 'filter_c

So apparently there is prior art for using hookname_.

In fact that is in:

function hook_theme_suggestions_HOOK_alter(array &$suggestions, array $variables) {
  if (empty($variables['header'])) {
    $suggestions[] = 'hookname__' . 'no_header';
  }
}

Which is the alter counterpart to this hook. So looks good to me.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.