You are here

amswap.module in Admin Menu Swap 7.2

Same filename and directory in other branches
  1. 8 amswap.module

Contains amswap.module.

File

amswap.module
View source
<?php

/**
 * @file
 * Contains amswap.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Menu\MenuTreeParameters;

/**
 * Implements hook_help().
 */
function amswap_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the amswap module.
    case 'help.page.amswap':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Allows the administration menu to be customised per user role.') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_toolbar_alter().
 */
function amswap_toolbar_alter(&$items) {

  // kint($items, '$items');
  $items['administration']['tray']['toolbar_administration']['#pre_render'][] = 'amswap_prerender_toolbar_administration_tray';
}

/**
 * Custom override of Toolbar module's function that renders the admin tray.
 */
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;
}

Functions

Namesort descending Description
amswap_help Implements hook_help().
amswap_prerender_toolbar_administration_tray Custom override of Toolbar module's function that renders the admin tray.
amswap_toolbar_alter Implements hook_toolbar_alter().