Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use Drupal 8 block region in template suggestions
<?php
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function THEMENAME_theme_suggestions_menu_alter(array &$suggestions, array $variables) {
if (isset($variables['attributes']['region'])) {
// Add a template suggestion based on region name
$suggestions[] = $variables['theme_hook_original'] . '__' . $variables['attributes']['region'];
}
}
/**
* Implements hook_preprocess_block().
*/
function THEMENAME_preprocess_block(&$variables) {
if (isset($variables['elements']['#id'])) {
$block_id = $variables['elements']['#id'];
$block = \Drupal\block\Entity\Block::load($block_id);
if ($block) {
// Add a region variable to a block.
$variables['content']['#attributes']['region'] = $block->getRegion();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment