You are here

function admin_toolbar_links_access_filter_preprocess_menu in Admin Toolbar 8.2

Same name and namespace in other branches
  1. 8 admin_toolbar_links_access_filter/admin_toolbar_links_access_filter.module \admin_toolbar_links_access_filter_preprocess_menu()
  2. 3.x admin_toolbar_links_access_filter/admin_toolbar_links_access_filter.module \admin_toolbar_links_access_filter_preprocess_menu()

Implements hook_preprocess_menu().

Hides links from admin menu, if user doesn't have access rights.

File

admin_toolbar_links_access_filter/admin_toolbar_links_access_filter.module, line 33
This module don't show menu links that you don't have access permission for.

Code

function admin_toolbar_links_access_filter_preprocess_menu(&$variables) {
  if (empty($variables['items'])) {

    // Additional empty check to prevent exotic situations, where the preprocess
    // function is entered even without items.
    // @see https://www.drupal.org/node/2833885
    return;
  }

  // Ensure that menu_name exists.
  if (!isset($variables['menu_name'])) {

    // In rare cases (for unknown reasons) menu_name may not be set.
    // As fallback, we can fetch it from the first menu item.
    $first_link = reset($variables['items']);

    /** @var Drupal\Core\Menu\MenuLinkDefault $original_link */

    // Fetch the menu_name from the original link.
    $original_link = $first_link['original_link'];
    $variables['menu_name'] = $original_link
      ->getMenuName();
  }
  if ($variables['menu_name'] == 'admin') {
    if (!admin_toolbar_links_access_filter_user_has_admin_role($variables['user'])) {
      admin_toolbar_links_access_filter_filter_non_accessible_links($variables['items']);
    }
  }
}