8.3.x theme.api.php hook_theme_suggestions_HOOK_alter(array &$suggestions, array $variables)
8.0.x theme.api.php hook_theme_suggestions_HOOK_alter(array &$suggestions, array $variables)
8.1.x theme.api.php hook_theme_suggestions_HOOK_alter(array &$suggestions, array $variables)
8.2.x theme.api.php hook_theme_suggestions_HOOK_alter(array &$suggestions, array $variables)
8.4.x theme.api.php hook_theme_suggestions_HOOK_alter(array &$suggestions, array $variables)

Alters named suggestions for a specific theme hook.

This hook allows any module or theme to provide alternative theme function or 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'].

@todo Add @code sample.

Parameters

array $suggestions: An array of theme suggestions.

array $variables: An array of variables passed to the theme hook. Note that this hook is invoked before any preprocessing.

See also

hook_theme_suggestions_alter()

hook_theme_suggestions_HOOK()

Related topics

11 functions implement hook_theme_suggestions_HOOK_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

bartik_theme_suggestions_form_alter in core/themes/bartik/bartik.theme
Implements hook_theme_suggestions_HOOK_alter() for form templates.
test_theme_theme_suggestions_theme_test_function_suggestions_alter in core/modules/system/tests/themes/test_theme/test_theme.theme
Implements hook_theme_suggestions_HOOK_alter().
test_theme_theme_suggestions_theme_test_suggestions_alter in core/modules/system/tests/themes/test_theme/test_theme.theme
Implements hook_theme_suggestions_HOOK_alter().
theme_suggestions_test_theme_suggestions_theme_test_function_suggestions_alter in core/modules/system/tests/modules/theme_suggestions_test/theme_suggestions_test.module
Implements hook_theme_suggestions_HOOK_alter().
theme_suggestions_test_theme_suggestions_theme_test_specific_suggestions_alter in core/modules/system/tests/modules/theme_suggestions_test/theme_suggestions_test.module
Implements hook_theme_suggestions_HOOK_alter().

... See full list

File

core/lib/Drupal/Core/Render/theme.api.php, line 713
Hooks and documentation related to the theme and render system.

Code

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

Comments

sudishth’s picture

for node
----------------
function mytheme_theme_suggestions_node_alter(array &$suggestions, array $variables) {
$product_types = array_keys(grnGetProducts());
$product_type = $variables['elements']['#node']->getType();
if ( $variables['elements']['#view_mode'] == 'full' && in_array($product_type, $product_types) ) {
$suggestions[] = 'node__' . 'products';
}
}

for page
-----------
function mytheme_theme_suggestions_page_alter(array &$suggestions, array $variables) {
$path_matcher = Drupal::service('path.matcher');
$suggestion_theme_settings = theme_get_setting('d8_template');
if ($suggestion_theme_settings != 'default') {
if ($path_matcher->isFrontPage()) {
$suggestions[] = $variables['theme_hook_original'] . '__front__d8__' . $suggestion_theme_settings;
}
$suggestions[] = $variables['theme_hook_original'] . '__d8__' . $suggestion_theme_settings;
}
return $suggestions;
}

CatsFromStonehenge’s picture

For Node

function mytheme_theme_suggestions_node_alter(array &$suggestions, array $variables) {
  $product_types = array_keys(grnGetProducts());
  $product_type = $variables['elements']['#node']->getType();
  if ( $variables['elements']['#view_mode'] == 'full' && in_array($product_type, $product_types) ) {
    $suggestions[] = 'node__' . 'products';
  }
}

For Page

function mytheme_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  $path_matcher = Drupal::service('path.matcher');
  $suggestion_theme_settings = theme_get_setting('d8_template');
  if ($suggestion_theme_settings != 'default') {
    if ($path_matcher->isFrontPage()) {
      $suggestions[] = $variables['theme_hook_original'] . '__front__d8__' . $suggestion_theme_settings;
    }
    $suggestions[] = $variables['theme_hook_original'] . '__d8__' . $suggestion_theme_settings;
  }
  return $suggestions;
}
jmickela’s picture

Nevermind....I would delete this comment, but it seems I can't...