function ThemeManager::buildThemeHookSuggestions

Same name in other branches
  1. 11.x core/lib/Drupal/Core/Theme/ThemeManager.php \Drupal\Core\Theme\ThemeManager::buildThemeHookSuggestions()

Builds theme hook suggestions for a theme hook with variables.

@internal This method may change at any time. It is not for use outside this class.

Parameters

string $hook: Theme hook that was called.

string $info_base_hook: Theme registry info for $hook['base hook'] key or empty string.

array $variables: Theme variables that were passed along with the call.

Return value

string[] Suggested theme hook names to use instead of $hook, in the order of ascending specificity. The caller will pick the last of those suggestions that has a known theme registry entry.

1 call to ThemeManager::buildThemeHookSuggestions()
ThemeManager::render in core/lib/Drupal/Core/Theme/ThemeManager.php

File

core/lib/Drupal/Core/Theme/ThemeManager.php, line 371

Class

ThemeManager
Provides the default implementation of a theme manager.

Namespace

Drupal\Core\Theme

Code

protected function buildThemeHookSuggestions(string $hook, string $info_base_hook, array &$variables) : array {
    // Set base hook for later use. For example if '#theme' => 'node__article'
    // is called, we run hook_theme_suggestions_node_alter() rather than
    // hook_theme_suggestions_node__article_alter(), and also pass in the base
    // hook as the last parameter to the suggestions alter hooks.
    $base_theme_hook = $info_base_hook ?: $hook;
    // Invoke hook_theme_suggestions_HOOK().
    $suggestions = $this->moduleHandler
        ->invokeAll('theme_suggestions_' . $base_theme_hook, [
        $variables,
    ]);
    // If the theme implementation was invoked with a direct theme suggestion
    // like '#theme' => 'node__article', add it to the suggestions array before
    // invoking suggestion alter hooks.
    if ($info_base_hook) {
        $suggestions[] = $hook;
    }
    // Invoke hook_theme_suggestions_alter() and
    // hook_theme_suggestions_HOOK_alter().
    $hooks = [
        'theme_suggestions',
        'theme_suggestions_' . $base_theme_hook,
    ];
    $this->moduleHandler
        ->alter($hooks, $suggestions, $variables, $base_theme_hook);
    $this->alter($hooks, $suggestions, $variables, $base_theme_hook);
    return $suggestions;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.