1

So what I am trying to do, is to get all the blocks in footer region to use the same template, say, block--footer-block.html.twig.

What I tried is to use hook_theme_suggestions_HOOK_alter() to check where the blocks are located and add the region name to the template suggestions.

/**
* Implements hook_theme_suggestions_HOOK_alter().
* */
function mytheme_theme_suggestions_block_alter(array &$suggestions, array 
$variables {
    $block = Block::load($variables['elements']);
    $region = $block->getRegion();
    $suggestions[] = 'block . '__' . $region . '__block';
}

Currently the $block->getRegion(); breaks my site - no idea why. It doesn't even give any errors, the site is just blank.

Is this even a good way of doing this?

Jaguarundi
  • 11
  • 2
  • I dont think `$block->getRegion();` is breaking it. It is the next line. Take a close look at your single quotes – Ronnie Aug 11 '17 at 23:21
  • Thanks for the input, but that line is actually commented out in my code at the moment, it's the line before where it breaks. But you are right, there is a mistake in the quotes! – Jaguarundi Aug 14 '17 at 05:56

2 Answers2

3

This was answered here: https://drupal.stackexchange.com/questions/192616/how-to-make-a-theme-hook-suggestion-for-blocks-according-to-region

function mytheme_theme_suggestions_block_alter(array &$suggestions, array $variables) {
  $block = Block::load($variables['elements']['#id']);
  $region = $block->getRegion();
  $suggestions[] = 'block__'.$region.'__block'; 
}
stacey.mosier
  • 393
  • 2
  • 8
0

You can also use this snippet, in your hook_theme_suggestions_HOOK_alter:

if (isset($variables['elements']['#configuration']['region'])) {
    $region = $variables['elements']['#configuration']['region'];
    $suggestions[] = 'block__' . $region;
    if (isset($variables['elements']['#configuration']['provider'])) {
      $provider = $variables['elements']['#configuration']['provider'];
      $suggestions[] = 'block__' . $region . '__' . $provider;
    }
  }

Reference

eliosh
  • 1
  • 2