You are here

function amswap_prerender_toolbar_administration_tray in Admin Menu Swap 7.2

Custom override of Toolbar module's function that renders the admin tray.

1 string reference to 'amswap_prerender_toolbar_administration_tray'
amswap_toolbar_alter in ./amswap.module
Implements hook_toolbar_alter().

File

./amswap.module, line 45
Contains amswap.module.

Code

function amswap_prerender_toolbar_administration_tray(array $element) {

  // kint($element, '$element');
  // Track if any menu was specified for this role.
  $menu_specified = FALSE;
  $toolbar_menu_tree = \Drupal::service('toolbar.menu_tree');

  // kint($toolbar_menu_tree, '$toolbar_menu_tree');
  // Load the administrative menu.
  $parameters = new MenuTreeParameters();
  $parameters
    ->onlyEnabledLinks();
  $currentUser = \Drupal::currentUser();
  $role_menu_pairs = \Drupal::config('amswap.amswapconfig')
    ->get('role_menu_pairs');

  // kint($role_menu_pairs, '$role_menu_pairs');
  foreach ($role_menu_pairs as $index => $pair) {

    // If the specified role matches the users current role
    if (in_array($pair['role'], $currentUser
      ->getRoles())) {
      $parameters
        ->setMinDepth(1)
        ->setMaxDepth(4);

      // kint($parameters);
      $tree = $toolbar_menu_tree
        ->load($pair['menu'], $parameters);

      // kint($tree, '$tree');
      $menu_specified = TRUE;
    }
  }

  // kint($menu_specified, '$menu_specified');
  if (!$menu_specified) {

    // Return un-altered
    return $element;
  }

  // Add manipulators
  $manipulators = [
    [
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ],
    [
      'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ],
  ];

  // If Admin Toolbar module is installed, use its toolbar manipulation function
  if (function_exists('toolbar_tools_menu_navigation_links')) {
    $manipulators[] = [
      'callable' => 'toolbar_tools_menu_navigation_links',
    ];
  }
  else {
    $manipulators[] = [
      'callable' => 'toolbar_menu_navigation_links',
    ];
  }
  $tree = $toolbar_menu_tree
    ->transform($tree, $manipulators);

  // kint($tree, '$tree');
  $element['administration_menu'] = $toolbar_menu_tree
    ->build($tree);
  return $element;
}