Skip to content

Instantly share code, notes, and snippets.

@davebeach
Last active June 12, 2017 06:10
Show Gist options
  • Save davebeach/2f29f64d48f8a502a07697ef8b491149 to your computer and use it in GitHub Desktop.
Save davebeach/2f29f64d48f8a502a07697ef8b491149 to your computer and use it in GitHub Desktop.
<?php
/**
* @author
* https://drupal.stackexchange.com/questions/192616/how-to-make-a-theme-hook-suggestion-for-blocks-according-to-region/192623#192623
*/
use Drupal\block\Entity\Block;
function MODULE_theme_suggestions_block_alter(array &$suggestions, array $variables) {
if (!empty($variables['elements']['#id'])) {
$block = Block::load($variables['elements']['#id']);
$suggestions[] = 'block__' . $block->getRegion() . '__' . $variables['elements']['#id'];
}
/* Use this 'else if' only if you are using page_manager module and want to know which region is the block */
else if (isset($variables['elements']['#configuration']['region'])) {
$suggestions[] = 'block__page_' . $variables['elements']['#configuration']['region'] . '__' . end(explode(':', $variables['elements']['#plugin_id']));
}
return $suggestions;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment