function hook_theme_suggestions_HOOK_alter
Same name and namespace in other branches
- 11.x core/lib/Drupal/Core/Render/theme.api.php \hook_theme_suggestions_HOOK_alter()
- 10 core/lib/Drupal/Core/Render/theme.api.php \hook_theme_suggestions_HOOK_alter()
- 9 core/lib/Drupal/Core/Render/theme.api.php \hook_theme_suggestions_HOOK_alter()
- 8.9.x core/lib/Drupal/Core/Render/theme.api.php \hook_theme_suggestions_HOOK_alter()
Alters named suggestions for a specific theme hook.
This hook allows any module or theme to provide alternative template name suggestions and reorder or remove suggestions provided by hook_theme_suggestions_HOOK() or by earlier invocations of this hook.
HOOK is the least-specific version of the hook being called. For example, if '#theme' => 'node__article' is called, then node_theme_suggestions_node() will be invoked, not node_theme_suggestions_node__article(). The specific hook called (in this case 'node__article') is available in $variables['theme_hook_original'].
New suggestions must begin with the value of HOOK, followed by two underscores to be discoverable. For example, consider the below suggestions from hook_theme_suggestions_node_alter:
- node__article is valid
- node__article__custom_template is valid
- node--article is invalid
- article__custom_template is invalid
Implementations of this hook must be placed in src/Hook and tagged with the #[Hook('theme_suggestions_HOOK_alter')] attribute.
In the following example, we provide an alternative template suggestion to node templates based on the user being logged in.
function MY_MODULE_theme_suggestions_node_alter(array &$suggestions, array $variables) {
if (\Drupal::currentUser()->isAuthenticated()) {
$suggestions[] = 'node__logged_in';
}
}
Parameters
array $suggestions: An array of theme suggestions, passed by reference.
array $variables: An array of variables passed to the theme hook, passed by reference. Note that this hook is invoked before any preprocessing.
See also
hook_theme_suggestions_alter()
Related topics
File
-
core/
lib/ Drupal/ Core/ Render/ theme.api.php, line 775
Code
function hook_theme_suggestions_HOOK_alter(array &$suggestions, array &$variables) {
if (empty($variables['header'])) {
$suggestions[] = 'hookname__no_header';
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.