Created
June 7, 2020 03:28
-
-
Save crittermike/7bb1f3862a10b2b6483345d7f8afd099 to your computer and use it in GitHub Desktop.
Use Drupal 8 block region in 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
<?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