You are here

function menu_item_extras_preprocess_block in Menu Item Extras 8

Same name and namespace in other branches
  1. 8.2 menu_item_extras.module \menu_item_extras_preprocess_block()

Implements hook_preprocess_block().

File

./menu_item_extras.module, line 52
Adds body field to the menu item.

Code

function menu_item_extras_preprocess_block(&$variables) {

  // Menus are built with #theme 'menu__MENU_NAME' form the the MenuLinkTree
  // class. We need to build menus supported by menu_item_extras with the
  // default #theme menu, to be able to add suggestions in the good order.
  if (isset($variables['content']['#menu_name'])) {
    $menu_name = $variables['content']['#menu_name'];
    $allowed_menus = \Drupal::configFactory()
      ->get('menu_item_extras.settings')
      ->get('allowed_menus');
    if (!empty($allowed_menus) && in_array($menu_name, $allowed_menus)) {
      $variables['content']['#theme'] = 'menu';

      // Pass region name to the suggestions_menu_alter for
      // the region suggestion.
      $block_id = $variables['elements']['#id'];

      /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $block_storage */
      $block_storage = \Drupal::entityTypeManager()
        ->getStorage('block');

      /** @var \Drupal\block\BlockInterface $block */
      $block = $block_storage
        ->load($block_id);
      $variables['content']['#attributes']['data-region'] = $block
        ->getRegion();
    }
  }
}