Increase depth limitation in Admin Toolbar

Table of contents

Admin Toolbar module is a wonderful module, a real must-have. But did you ever notice the fourth-level depth limitation while rendering the Admin Toolbar dropdown menu ?

The module limits the rendering of the dropdown toolbar menu to at most a four-level hierarchy, and this limitation is hardcoded. That makes some shortcuts inaccessible via the toolbar, leveraging its interest with deep level configurations module, such as Drupal Commerce for instance. In the picture below, you see no quick access to stores types and its fields settings. It means more clicks in the UI will be required to access it.

Admin Toolbar is limited to level depth 4 at most
Admin Toolbar module limits the toolbar menu to depth level 4 at most

In this short Drupal trick, we will show how to override properly this limitation, without hacking Admin Toolbar code.

1. Override Admin Toolbar element renderer

First thing first, we have to notice where is the hardcoded limitation involved. It is rather easy to find searching the proper function name in code and you will notice the \Drupal\admin_toolbar\Render\Element\AdminToolbar class and its method preRenderTray(). This method is responsible to build the admin menu as a tree and it hardcodes the depth limitation via the usage of setMaxDepth(4).

To change this, we simply have to mimic this file in a custom module by defining a new \Drupal\custom_module\Render\Element\ExpandedAdminToolbar class in /src/Render/Element/ExpandedAdminToolbar.php file. Extend the original class and override the preRenderTray() method by simply duplicating its code and changing the call to setMaxDepth() method to a greater limit.

Here is the resulting source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
 
namespace Drupal\custom_module\Render\Element;
 
use Drupal\admin_toolbar\Render\Element\AdminToolbar;
use Drupal\Core\Menu\MenuTreeParameters;
 
/**
 * Class ExtendedAdminToolbar.
 *
 * @package Drupal\custom_module\Render\Element
 */
class ExtendedAdminToolbar extends AdminToolbar {
 
  /**
   * Renders the toolbar's administration tray.
   *
   * This is a clone of AdminToolbar:preRenderTray() which uses setMaxDepth() to
   * a deeper level as Admin Toolbar only goes down to level 4.
   *
   * @param array $build
   *   A renderable array.
   *
   * @return array
   *   The updated renderable array.
   *
   * @see \Drupal\admin_toolbar\Render\Element\AdminToolbar:preRenderTray()
   */
  public static function preRenderTray(array $build) {
    $menu_tree = \Drupal::service('toolbar.menu_tree');
    $parameters = new MenuTreeParameters();
    // NOTE: here we hardcode max depth to 10, but :
    // - you could remove the method to unlimit depth
    // - you could set the limitation to a depth chosen in a settings form
    $parameters->setRoot('system.admin')->excludeRoot()->setMaxDepth(10)->onlyEnabledLinks();
    $tree = $menu_tree->load(NULL, $parameters);
    $manipulators = [
      ['callable' => 'menu.default_tree_manipulators:checkAccess'],
      ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
      ['callable' => 'toolbar_tools_menu_navigation_links'],
    ];
    $tree = $menu_tree->transform($tree, $manipulators);
    $build['administration_menu'] = $menu_tree->build($tree);
    return $build;
  }
}

2. Use the new file instead of the original one

The last and remaining task is to force Drupal to use our own method rather then Admin Toolbar original one. To do so, we must alter the took to register our method. The following code goes to custom_module.module file.

1
2
3
4
5
6
/**
 * Implements hook_toolbar_alter().
 */
function custom_module_toolbar_alter(&$items) {
  $items['administration']['tray']['toolbar_administration']['#pre_render'] = [[\Drupal\custom_module\Render\Element\ExtendedAdminToolbar::class, 'preRenderTray']];
}

That's it ! We just used hook_toolbar_alter() to change the config. Clear you cache eventually and enjoy your new extended dropdown admin toolbar.

Expanded Admin Toolbar with a greater depth limitation
We can now access deeper shortcuts in the menu structure

Add new comment

Your name will be publicly displayed along with your comment.
Your email will be kept private and only used to notify you.
On internet, you can be who you want. Please be someone nice :)