menu_block-8.x-1.x-dev/menu_block.module

menu_block.module
<?php

/**
 * @file
 * Provides configurable blocks of menu links.
 */

/**
 * Implements hook_theme_suggestions_HOOK() for "block".
 */
function menu_block_theme_suggestions_block(array $variables) {
  $suggestions = [];

  // Check if this is a menu_block block.
  if (isset($variables['elements']['#base_plugin_id']) && $variables['elements']['#base_plugin_id'] == 'menu_block') {
    $menu_name = strtr($variables['elements']['#derivative_plugin_id'], '-', '_');
    $config = $variables['elements']['#configuration'] ?? [];

    // Context module (and perhaps others?) adds 'region' into the config.
    if (!empty($config['region'])) {
      $suggestions[] = 'block__menu_block__region_' . $config['region'];
      $suggestions[] = 'block__menu_block__' . $menu_name . '__region_' . $config['region'];
    }

    // Add our custom theme suggestion.
    if (!empty($config['suggestion']) && $config['suggestion'] !== $menu_name) {
      $suggestions[] = 'block__menu_block__' . $config['suggestion'];
    }

    // Context module adds block 'uuid' into the config.
    if (!empty($config['uuid'])) {
      $suggestions[] = 'block__menu_block__' . strtr($config['uuid'], '-', '_');
    }
  }

  return $suggestions;
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 *
 * Adds block__system_menu_block so menu blocks work the same as core's menu
 * blocks.
 */
function menu_block_theme_suggestions_block_alter(array &$suggestions, array $variables) {
  if ($suggestions[0] == 'block__menu_block') {
    if (isset($suggestions[1]) && $suggestions[1] == 'block__menu_block') {
      // Since this first suggestion is a dupe, replace it with the system
      // suggestion.
      $suggestions[0] = 'block__system_menu_block';
    }
    // If some other module has removed the duplicates, use array_unshift().
    else {
      array_unshift($suggestions, 'block__system_menu_block');
    }
    // The suggestions added with menu_block_theme_suggestions_block() are added
    // after the machine name-based suggestion, but are less specific and should
    // come before it.
    if (!empty($variables['elements']['#id'])) {
      $machine_name_suggestion = 'block__' . $variables['elements']['#id'];
      $suggestions = array_diff($suggestions, [$machine_name_suggestion]);
      $suggestions[] = $machine_name_suggestion;
    }
  }
}

/**
 * Implements hook_theme_registry_alter().
 */
function menu_block_theme_registry_alter(&$theme_registry) {
  // Add $menu_block_configuration as a variable to the 'menu' theme hook. Set
  // its default value to be an empty array.
  $theme_registry['menu']['variables']['menu_block_configuration'] = [];
}

/**
 * Implements hook_theme_suggestions_HOOK() for "menu".
 */
function menu_block_theme_suggestions_menu(array $variables) {
  $suggestions = [];

  // The MenuBlock plugin's build() method populates this variable.
  if (!empty($variables['menu_block_configuration'])) {
    $config = $variables['menu_block_configuration'];
    $menu_name = strtr($variables['menu_name'], '-', '_');

    $suggestions[] = 'menu__' . $menu_name;

    // Context module (and perhaps others?) adds 'region' into the config.
    if (!empty($config['region'])) {
      $suggestions[] = 'menu__region_' . $config['region'];
      $suggestions[] = 'menu__' . $menu_name . '__region_' . $config['region'];
    }

    // Add our custom theme suggestion.
    if (!empty($config['suggestion']) && $config['suggestion'] !== $menu_name) {
      $suggestions[] = 'menu__' . $config['suggestion'];
    }

    // Context module adds block 'uuid' into the config.
    if (!empty($config['uuid'])) {
      $suggestions[] = 'menu__' . $menu_name . '__' . $config['uuid'];
    }
  }

  return $suggestions;
}

/**
 * Implements hook_preprocess_hook() for "block".
 *
 * Set the block label with the #menu_block_configuration label if it exists.
 *
 * @see template_preprocess_block()
 */
function menu_block_preprocess_block(&$variables) {
  if (isset($variables['content']['#menu_block_configuration']['label'])) {
    $config_label = $variables['content']['#menu_block_configuration']['label'];
    $token = \Drupal::token();
    // If the $config_label doesn't contain token, we set the label.
    if (empty($token->scan($config_label))) {
      // Set the 'label' template variable to an empty string if the block is
      // configured not to display a label.
      $variables['label'] = empty($variables['configuration']['label_display']) ? '' : $config_label;
      // However, we always set the configuration label (regardless of the
      // 'label_display' setting) so the label can be included in the markup as
      // hidden text for assistive technologies (for templates that handle
      // this).
      $variables['configuration']['label'] = $config_label;
    }
  }
}

/**
 * Implements hook_plugin_filter_TYPE_alter().
 *
 * Suppress display of system blocks in Block Library & Layout Builder
 * when menu_blocks equivalents are present.
 */
function menu_block_plugin_filter_block_alter(array &$definitions, array $extra, $consumer) {
  if (in_array($consumer, ['block_ui', 'layout_builder'])) {
    foreach ($definitions as $id => $definition) {
      // Is this a core-provided menu block?
      if ($definition['provider'] === 'system' && strpos($id, 'system_menu_block:') !== FALSE) {
        // Extract the machine name of the menu.
        $split_system_block_name = explode(':', $id);
        // Generate the menu_block equivalent key to compare.
        $menu_block_name = 'menu_block:' . $split_system_block_name[1];
        // If a menu_block equivalent exists, suppress the core menu from
        // being displayed as an available option.
        if (in_array($menu_block_name, array_keys($definitions))) {
          unset($definitions[$id]);
        }
      }
    }
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc