Skip to content
Snippets Groups Projects
Unverified Commit 81ea13c9 authored by Pablo López's avatar Pablo López
Browse files

Issue #3415572: Add hoook based approach for altering NavigationMenuLinkTree

parent 7f1f6f15
No related branches found
No related tags found
1 merge request!8026Issue #3415572: Adjust how Help link and Content links are removed from the Administration menu for the Navigation bar
<?php
/**
* @file
* Hooks specific to the Navigation module.
*/
/**
* @addtogroup hooks
* @{
*/
/**
* Alter the content of a given Navigation menu link tree.
*
* @param array &$tree
* The Navigation link tree.
*
* @see \Drupal\navigation\Menu\NavigationMenuLinkTree::transform()
*/
function hook_navigation_menu_link_tree_alter(array &$tree): void {
foreach ($tree as $key => $item) {
// Skip elements where menu is not the 'admin' one.
$menu_name = $item->link->getMenuName();
if ($menu_name != 'admin') {
continue;
}
// Remove unwanted Help menu link.
$plugin_id = $item->link->getPluginId();
if ($plugin_id == 'help.main') {
unset($tree[$key]);
}
}
}
/**
* @} End of "addtogroup hooks".
*/
......@@ -27,14 +27,10 @@ services:
'@router.route_provider',
'@menu.active_trail',
'@callable_resolver',
'@module_handler',
]
navigation.user_lazy_builder:
class: Drupal\navigation\UserLazyBuilder
arguments: ['@module_handler', '@current_user']
Drupal\navigation\UserLazyBuilders: '@navigation.user_lazy_builder'
navigation.navigation_tree_manipulators:
class: Drupal\navigation\NavigationMenuLinkTreeManipulators
arguments: ['@module_handler']
Drupal\navigation\NavigationMenuLinkTreeManipulators: '@navigation.navigation_tree_manipulators'
<?php
declare(strict_types=1);
namespace Drupal\navigation\Hook;
use Drupal\Core\Hook\Attribute\Hook;
/**
* Hook implementations for the navigation module.
*/
class NavigationHooks {
/**
* Implements hook_navigation_menu_link_tree_alter().
*/
#[Hook('navigation_menu_link_tree_alter')]
public function navigationMenuLinkTreeAlter(array &$tree): void {
foreach ($tree as $key => $item) {
// Skip elements where menu is not the 'admin' one.
$menu_name = $item->link->getMenuName();
if ($menu_name != 'admin') {
continue;
}
// Remove unwanted Help and Content menu links.
$plugin_id = $item->link->getPluginId();
if ($plugin_id == 'help.main' || $plugin_id == 'system.admin_content') {
unset($tree[$key]);
}
// Remove child items of content menu, if any.
$parent = $item->link->getParent();
if ($parent == 'system.admin_content') {
unset($tree[$key]);
}
}
}
}
......@@ -4,7 +4,13 @@
namespace Drupal\navigation\Menu;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Menu\MenuActiveTrailInterface;
use Drupal\Core\Menu\MenuLinkManagerInterface;
use Drupal\Core\Menu\MenuLinkTree;
use Drupal\Core\Menu\MenuTreeStorageInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\Utility\CallableResolver;
/**
* Extends MenuLinkTree to add specific theme suggestions for the navigation.
......@@ -13,6 +19,33 @@
*/
final class NavigationMenuLinkTree extends MenuLinkTree {
/**
* Constructs a \Drupal\navigation\Menu\NavigationMenuLinkTree object.
*
* @param \Drupal\Core\Menu\MenuTreeStorageInterface $treeStorage
* The menu link tree storage.
* @param \Drupal\Core\Menu\MenuLinkManagerInterface $menuLinkManager
* The menu link plugin manager.
* @param \Drupal\Core\Routing\RouteProviderInterface $routeProvider
* The route provider to load routes by name.
* @param \Drupal\Core\Menu\MenuActiveTrailInterface $menuActiveTrail
* The active menu trail service.
* @param \Drupal\Core\Utility\CallableResolver $callableResolver
* The callable resolver.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* The module handler.
*/
public function __construct(
MenuTreeStorageInterface $treeStorage,
MenuLinkManagerInterface $menuLinkManager,
RouteProviderInterface $routeProvider,
MenuActiveTrailInterface $menuActiveTrail,
CallableResolver $callableResolver,
protected ModuleHandlerInterface $moduleHandler,
) {
parent::__construct($treeStorage, $menuLinkManager, $routeProvider, $menuActiveTrail, $callableResolver);
}
/**
* {@inheritdoc}
*/
......@@ -47,4 +80,13 @@ public function build(array $tree): array {
return $build;
}
/**
* {@inheritdoc}
*/
public function transform(array $tree, array $manipulators) {
$tree = parent::transform($tree, $manipulators);
$this->moduleHandler->alter('navigation_menu_link_tree', $tree);
return $tree;
}
}
<?php
declare(strict_types=1);
namespace Drupal\navigation;
use Drupal\Core\Extension\ModuleHandlerInterface;
/**
* Provides a menu link tree manipulators.
*
* This class provides menu link tree manipulators to:
* - trigger generic navigation link tree manipulate event,
*/
final class NavigationMenuLinkTreeManipulators {
/**
* Constructs a NavigationMenuLinkTreeManipulators object.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* The module handler.
*/
public function __construct(
private readonly ModuleHandlerInterface $moduleHandler,
) {}
/**
* @todo Add method description.
*/
public function manipulate(array $tree): array {
$this->moduleHandler->alter('navigation_menu_link_tree_manipulators', $tree);
return $tree;
}
}
......@@ -86,7 +86,6 @@ public function build(): array {
$manipulators = [
['callable' => 'menu.default_tree_manipulators:checkAccess'],
['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
['callable' => 'navigation.navigation_tree_manipulators:manipulate'],
];
$tree = $this->menuTree->transform($tree, $manipulators);
$build = $this->menuTree->build($tree);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment