Skip to content

Instantly share code, notes, and snippets.

@socketwench
Created November 9, 2020 04:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save socketwench/229fe15a7f20191505bba6082ffc5804 to your computer and use it in GitHub Desktop.
Save socketwench/229fe15a7f20191505bba6082ffc5804 to your computer and use it in GitHub Desktop.
Drupal 8: Add view_mode to block templates, add view mode to template suggestions.
/**
* Implements hook_preprocess_HOOK().
*/
function yourtheme_preprocess_block(&$variables) {
$content = $variables['elements']['content'];
if (isset($content['#block_content']) && $content['#block_content'] instanceof BlockContentInterface) {
$variables['view_mode'] = $content['#view_mode'];
}
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function yourtheme_theme_suggestions_block_alter(array &$suggestions, array $variables) {
$content = $variables['elements']['content'];
if (isset($content['#block_content']) && $content['#block_content'] instanceof BlockContentInterface) {
$suggestions[] = 'block__' . $content['#block_content']->bundle() . '__' . $content['#view_mode'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment