Skip to content

Instantly share code, notes, and snippets.

@crittermike
Created June 7, 2020 03:28
Show Gist options
  • Save crittermike/7bb1f3862a10b2b6483345d7f8afd099 to your computer and use it in GitHub Desktop.
Save crittermike/7bb1f3862a10b2b6483345d7f8afd099 to your computer and use it in GitHub Desktop.
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