You are here

function twigsuggest_theme_suggestions_block in Twig Template Suggester 8

Implements hook_theme_suggestions_HOOK() for block templates.

  • Suggest region-specific block templates.

Note: To suggest block type (bundle) specific block templates, use Block Type Templates module, https://www.drupal.org/project/block_type_templates

@TODO https://www.drupal.org/project/twigsuggest/issues/3007447

File

./twigsuggest.module, line 20
Twig Template Suggester module hook implementations.

Code

function twigsuggest_theme_suggestions_block(array $variables) {
  $suggestions = [];

  // Prevent PHP notices if contrib modules create blocks without this ID.
  if (isset($variables['elements']['#id']) || !empty($variables['elements']['#id'])) {
    if ($block = Block::load($variables['elements']['#id'])) {
      $suggestions[] = 'block__' . $block
        ->getRegion();
      $suggestions[] = 'block__' . $block
        ->getRegion() . '__' . $variables['elements']['#id'];
      if ($block
        ->get('settings') && ($provider = $block
        ->get('settings')['provider'])) {

        // I'm pretty sure core is already providing provider as a suggestion.
        // $suggestions[] = 'block__' . $provider;.
        $base_plugin = $variables['elements']['#base_plugin_id'];
        if ($base_plugin !== $provider) {
          $suggestions[] = 'block__' . $base_plugin;
        }
        $suggestions[] = 'block__' . $provider . '__' . $block
          ->getRegion();
        if ($variables['elements']['#base_plugin_id'] != $provider) {
          $suggestions[] = 'block__' . $base_plugin . '__' . $block
            ->getRegion();
        }
        if (isset($variables['elements']['content']['#menu_name'])) {
          $menu_name = str_replace("-", "_", $variables['elements']['content']['#menu_name']);
          $suggestions[] = 'block__' . $provider . '__' . $menu_name . '__' . $block
            ->getRegion();
          if ($base_plugin !== $provider) {
            $suggestions[] = 'block__' . $base_plugin . '__' . $menu_name . '__' . $block
              ->getRegion();
          }
        }
      }
    }
  }
  return $suggestions;

  // @TODO decide whether to prefix these suggestions.  Probably best to be
  // consistent but if i were to *not* prefix one, it would actually probably be
  // bundles (content type, block type, etc.).  Block Type Templates gets around
  // this by prefacing its suggestions more intelligibly with 'block-content'
  // (as types only applies to custom, content blocks) so giving it suggestions
  // like this: block--block-content-{{ machine-name-of-block-type }}.html.twig
  // but the code floating out there everywhere from Jeff Burnz (he recommends
  // Block Type Templates module over this earlier code snippet of his) does
  // preface with bundle:
  // if (isset($variables['elements']['content']['#block_content'])) {
  // array_splice($suggestions, 1, 0, 'block__bundle__' .
  // $variables['elements']['content']['#block_content']->bundle());
  // }
}