navigation_extra-1.0.x-dev/navigation_extra.module

navigation_extra.module
<?php

/**
 * @file
 * Primary module hooks for navigation module.
 */

use Drupal\Component\Utility\DeprecationHelper;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Component\Plugin\PluginBase;
use Drupal\navigation\Plugin\SectionStorage\NavigationSectionStorage;

/**
 * Implements hook_help().
 */
function navigation_extra_help($route_name, RouteMatchInterface $route_match): string {
  switch ($route_name) {
    case 'help.page.navigation_extra':
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Navigation extra module provides extra settings for the sidebar navigation.') . '</p>';
      $output .= '<p>' . t('For more information, see the <a href=":docs">online documentation for the Navigation extra module</a>.', [
        ':docs' => 'https://www.drupal.org/project/navigation_extra',
      ]) . '</p>';
      return $output;
  }

  return '';
}

/**
 * Implements hook_module_implements_alter().
 */
function navigation_extra_module_implements_alter(&$implementations, $hook): void {

  // Only consider implementations we deal with.
  if (!isset($implementations['navigation_extra'])) {
    return;
  }

  if ($hook == 'plugin_filter_block__layout_builder_alter') {
    // We take over the core navigation module hook implementation for this one.
    unset($implementations['navigation']);
    return;
  }

  // We need to run last...
  $settings = $implementations['navigation_extra'];
  unset($implementations['navigation_extra']);
  $implementations['navigation_extra'] = $settings;
}

/**
 * Implements hook_plugin_filter_TYPE__CONSUMER_alter().
 */
function navigation_extra_plugin_filter_block__layout_builder_alter(array &$definitions, array $extra): void {

  if (($extra['section_storage'] ?? NULL) instanceof NavigationSectionStorage) {

    // Keep track of what blocks we consider safe for navigation.
    $navigation_safe = [
      'navigation_extra_local_tasks',
      'navigation_extra_version',
    ];

    /** @var \Drupal\navigation_extra\NavigationExtraPluginManagerInterface $navigationExtra */
    $navigationExtra = \Drupal::service('navigation_extra.manager');
    if ($navigationExtra->isEnabled('blocks')) {
      $config = \Drupal::config('navigation_extra.settings');
      $block_list = explode("\n", $config->get("plugins.blocks.navigation_safe_block_ids") ?? '');
      $navigation_safe = array_filter(array_unique(array_merge(
        $navigation_safe,
        $block_list
      )));
    }

    $safe_definitions = array_filter($definitions, static function (array $definition, string $plugin_id) use ($navigation_safe): bool {
      [$base_plugin_id] = explode(PluginBase::DERIVATIVE_SEPARATOR, $plugin_id);
      return in_array($base_plugin_id, $navigation_safe, TRUE);
    }, ARRAY_FILTER_USE_BOTH);

    // Ensure compatibility with Drupal 11.
    DeprecationHelper::backwardsCompatibleCall(
      currentVersion: \Drupal::VERSION,
      deprecatedVersion: '10.3',
      currentCallable: function () use (&$definitions, $extra) {
        if (class_exists('\Drupal\navigation\Hook\NavigationHooks')) {
          $navigation_hooks = new \Drupal\navigation\Hook\NavigationHooks();
          $navigation_hooks->pluginFilterLayoutLayoutBuilderAlter($definitions, $extra);
        }
      },
      deprecatedCallable: function () use (&$definitions, $extra) {
        if (function_exists('navigation_plugin_filter_block__layout_builder_alter')) {
          navigation_plugin_filter_block__layout_builder_alter($definitions, $extra);
        }
      },
    );

    // Add the definitions we consider safe again.
    $definitions += $safe_definitions;
  }
}

/**
 * Implements hook_block_alter().
 */
function navigation_extra_block_alter(&$definitions): void {

  $hidden = [
    'navigation_extra_local_tasks',
    'navigation_extra_version',
  ];

  // Navigation extras config.
  $config = \Drupal::config('navigation_extra.settings');

  /** @var \Drupal\navigation_extra\NavigationExtraPluginManagerInterface $navigationExtra */
  $navigationExtra = \Drupal::service('navigation_extra.manager');
  if ($navigationExtra->isEnabled('blocks')) {
    $hidden += explode("\n", $config->get("plugins.blocks.hidden_navigation_safe_block_ids") ?? '');
  }

  foreach ($hidden as $block_id) {
    if (isset($definitions[$block_id])) {
      $definitions[$block_id]['_block_ui_hidden'] = TRUE;
    }
  }

  // Swap out the navigation menu block type with our class.
  if ($config->get("plugins.common.override_max_menu_depth") ?? 0) {
    foreach ($definitions as $block_id => &$definition) {
      if (!str_starts_with($block_id, 'navigation_menu')) {
        continue;
      }
      $definition['class'] = 'Drupal\navigation_extra\Plugin\Block\NavigationMenuBlockOverride';
    }
  }
}

/**
 * Implements hook_page_attachments().
 */
function navigation_extra_page_attachments(&$page): void {
  /** @var \Drupal\navigation_extra\NavigationExtraPluginManagerInterface $navigationExtra */
  $navigationExtra = \Drupal::service('navigation_extra.manager');
  $navigationExtra->pageAttachments($page);
}

/**
 * Implements hook_menu_links_discovered_alter().
 */
function navigation_extra_menu_links_discovered_alter(&$links): void {
  /** @var \Drupal\navigation_extra\NavigationExtraPluginManagerInterface $navigationExtra */
  $navigationExtra = \Drupal::service('navigation_extra.manager');
  $navigationExtra->menuLinksDiscoveredAlter($links);
}

/**
 * Implements hook_preprocess_menu().
 */
function navigation_extra_preprocess_menu(&$variables): void {

  if (
    empty($variables['items'])
    || !isset($variables['menu_name'])
    || !in_array($variables['menu_name'], ['admin', 'navigation_extra_version', 'content'])
  ) {
    return;
  }

  /** @var \Drupal\navigation_extra\NavigationExtraPluginManagerInterface $navigationExtra */
  $navigationExtra = \Drupal::service('navigation_extra.manager');
  $navigationExtra->preprocessMenu($variables);
}

/**
 * Implements hook_element_info_alter().
 */
function navigation_extra_element_info_alter(&$types): void {
  array_unshift($types['view']['#pre_render'], [
    'Drupal\navigation_extra\AlternativeContentView',
    'preRender',
  ]);
}

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

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