You are here

block_type_templates.module in Block Type Templates 8

Provides the necessary hooks for the block_type_templates module.

File

block_type_templates.module
View source
<?php

/**
 * @file
 * Provides the necessary hooks for the block_type_templates module.
 */
use Drupal\block_content\BlockContentInterface;

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function block_type_templates_theme_suggestions_block_alter(array &$suggestions, array $variables) {
  $content = $variables['elements']['content'];
  if (isset($content['#block_content']) and $content['#block_content'] instanceof BlockContentInterface) {

    // Add 'block--block-content-BLOCK-TYPE.html.twig'.
    $block_type_suggestions[] = 'block__block_content_' . $content['#block_content']
      ->bundle();

    // Add 'block--block-content-BLOCK-TYPE--VIEW-MODE.html.twig'.
    $block_type_suggestions[] = 'block__block_content_' . $content['#block_content']
      ->bundle() . '__' . $content['#view_mode'];

    // Because block__block_content exists twice in $suggestions,
    // the suggestion arrays are reversed for further processing.
    $suggestions_rev = array_reverse($suggestions);
    $block_type_suggestions = array_reverse($block_type_suggestions);

    // Insert the block type and view mode suggestions between
    // block__block_content and the block instance-specific suggestions.
    $index = array_search('block__block_content', $suggestions_rev);
    if (is_numeric($index)) {
      array_splice($suggestions_rev, $index, 0, $block_type_suggestions);
      $suggestions = array_reverse($suggestions_rev);
    }
    else {
      $suggestions_rev = array_merge($suggestions_rev, $block_type_suggestions);
      $suggestions = array_reverse($suggestions_rev);
    }
  }
}