You are here

function menu_item_extras_preprocess_block in Menu Item Extras 8.2

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

Implements hook_preprocess_block().

File

./menu_item_extras.module, line 132
Manage fields for the menu items.

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']['#theme'], $variables['content']['#menu_name']) && (strpos($variables['content']['#theme'], 'menu__') === 0 || $variables['content']['#theme'] === 'menu') && Utility::checkBundleHasExtraFieldsThanEntity('menu_link_content', $variables['content']['#menu_name'])) {
    $variables['content']['#theme'] = 'menu';

    // Pass region name to the suggestions_menu_alter for
    // the region suggestion.
    if (!empty($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($variables['elements']['#id']);
      if (!empty($block)) {
        $variables['content']['#attributes']['data-region'] = $block
          ->getRegion();
      }
    }
  }
}