hook theme suggestions drupal 8 code example

Example 1: drupal 8 suggestions

function MYMODULE_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
  if (\Drupal::currentUser()
    ->isAuthenticated() && in_array($hook, array(
    'node',
    'taxonomy_term',
  ))) {
    $suggestions[] = $hook . '__' . 'logged_in';
  }
}

Example 2: theme hook suggestion for container drupal 8

/**
 * Implements hook_theme_suggestions_alter() for contact form container.
 */
function THEMENAME_theme_suggestions_container_alter(array &$suggestions, array &$variables)
{
  $name = '';
  $type = '';
  if (isset($variables['element']['#name'])) {
    $name = $variables['element']['#name'];
  }

  if (isset($variables['element']['#type'])) {
    $type = $variables['element']['#type'];
  }

  array_unshift($suggestions, 'container__' . $type . '__' . $name);
}

Tags:

Misc Example