Created
November 9, 2020 04:37
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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