Skip to content
Snippets Groups Projects

Make compatible with Gin Toolbar

Open Dieter Holvoet requested to merge issue/amswap-3258035:3258035 into 8.x-1.x
2 files
+ 32
7
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -30,10 +30,18 @@ class Amswap implements TrustedCallbackInterface {
public static function preRender(array $element) {
$parameters = new MenuTreeParameters();
$parameters->onlyEnabledLinks();
$parameters->setMinDepth(1);
$parameters->setMaxDepth(4);
$toolbar_menu_tree = \Drupal::service('toolbar.menu_tree');
$current_user = \Drupal::currentUser();
$role_menu_pairs = \Drupal::config('amswap.amswapconfig')->get('role_menu_pairs');
$active_trail = \Drupal::service('menu.active_trail');
if (\Drupal::moduleHandler()->moduleExists('gin_toolbar')) {
// If Gin Toolbar is installed, enable their custom active trail
$active_trail = \Drupal::service('gin_toolbar.active_trail');
}
$menu_specified = FALSE;
$trees = [];
@@ -41,10 +49,8 @@ class Amswap implements TrustedCallbackInterface {
foreach ($role_menu_pairs as $pair) {
// If the specified role matches the users current role
if (in_array($pair['role'], $current_user->getRoles(), FALSE)) {
$parameters
->setMinDepth(1)
->setMaxDepth(4);
$parameters->setActiveTrail($active_trail->getActiveTrailIds($pair['menu']));
$element['#cache']['contexts'][] = 'route.menu_active_trails:' . $pair['menu'];
$trees[] = $toolbar_menu_tree->load($pair['menu'], $parameters);
$menu_specified = TRUE;
}
@@ -52,8 +58,10 @@ class Amswap implements TrustedCallbackInterface {
}
if (!$menu_specified) {
// Return un-altered
return $element;
$parameters->setRoot('system.admin');
$parameters->setActiveTrail($active_trail->getActiveTrailIds('admin'));
$element['#cache']['contexts'][] = 'route.menu_active_trails:admin';
$trees[] = $toolbar_menu_tree->load('admin', $parameters);
}
// Add manipulators
@@ -71,6 +79,11 @@ class Amswap implements TrustedCallbackInterface {
$manipulators[] = ['callable' => 'toolbar_menu_navigation_links'];
}
if (\Drupal::moduleHandler()->moduleExists('gin_toolbar')) {
// If Gin Toolbar is installed, use its toolbar manipulation function
$manipulators[] = ['callable' => 'gin_toolbar_tools_menu_navigation_links'];
}
$element['administration_menu'] = [];
foreach ($trees as $tree) {
$tree = $toolbar_menu_tree->transform($tree, $manipulators);
Loading