0

I'd like to hide some menu on the backend side /admin/structure/menu and make the hidden items to disappear of the navigation menu for a specific user/role.

I managed to hide the list of the menu visible in the page /admin/structure/menu but it's just the list which as been changed, meaning they can access by accessing to the url.

function module_menu_alter(&$items) {
    $items['admin/structure/menu/manage/devel']['page callback'] = 'module_change_devel';
    $items['admin/structure/menu']['page callback'] = 'module_change_menus';
}

function module_change_menus() {

    global $user;
    if(!in_array('administrator', $user->roles)){
        $result = db_query("SELECT * FROM {menu_custom} 
            WHERE menu_name='menu-contact-us' 
            OR menu_name='menu-1' 
            OR menu_name = 'menu-3' 
            OR menu_name = 'menu-2' 
            ORDER BY title", 
            array(), 
            array('fetch' => PDO::FETCH_ASSOC));
    }
    else{
       $result = db_query("SELECT * FROM {menu_custom} 
           ORDER BY title", 
           array(), 
           array('fetch' => PDO::FETCH_ASSOC)); 
    }
    $rows = array();
    $header = array(t('Title'), array('data' => t('Operations'), 'colspan' => '3'));
    foreach ($result as $menu) {
    $row = array(theme('menu_admin_overview', array('title' => $menu['title'], 'name' => $menu['menu_name'], 'description' => $menu['description'])));
    $row[] = array('data' => l(t('list links'), 'admin/structure/menu/manage/' . $menu['menu_name']));
    $row[] = array('data' => l(t('edit menu'), 'admin/structure/menu/manage/' . $menu['menu_name'] . '/edit'));
    $row[] = array('data' => l(t('add link'), 'admin/structure/menu/manage/' . $menu['menu_name'] . '/add'));
    $rows[] = $row;
    }
    return theme('table', array('header' => $header, 'rows' => $rows));

}


function menu_change_devel() {

    global $user;
    if(in_array('administrator', $user->roles) == 1){
        $items['admin/structure/menu/manage/devel']['access arguments'] =  array('aminister users');
        return $items;
    }
}

But the function menu_change_devel which is supposed to give the access to admin/structure/menu/manage/devel for the user having the permission to aminister users doesn't seem to work :(

Any idea ?

Cheers

EDIT : I found the solution

function MODULE_menu_alter(&$items) {

    //deny the menu if they are not admin (if they don't have the acess right to adsminister users)
    $items['admin/structure/menu/manage/%menu']['access callback'] = array('MODULE_check_menus_access');
    $items['admin/structure/menu/manage/%menu']['access arguments'] = array(4, 'administer menu');
    //edit
    $items['admin/structure/menu/manage/%menu/edit']['access callback'] = array('MODULE_check_menus_access');
    $items['admin/structure/menu/manage/%menu/edit']['access arguments'] = array(4, 'administer menu');
    //add
    $items['admin/structure/menu/manage/%menu/add']['access callback'] = array('MODULE_check_menus_access');
    $items['admin/structure/menu/manage/%menu/add']['access arguments'] = array(4, 'administer menu');
}

function MODULE_check_menus_access($menu, $permission){
    global $user;
    $account = $user;

    if (user_access($permission, $account)) {
        if(array_key_exists ($menu['menu_name'], $menuList)){
            return user_access('can manage '.$menu['menu_name'], $account);
        }
        else{
            return true;
        }
    }
}

/**
 * Implements hook_permission().
 */
function MODULE_permission() {
  return array(
    'can manage navigation' =>  array(
      'title' => t('Can manage navigation menu'),
      'description' => t('Limit access to the navigation menu'),
      ),
    );
}
  • How can this be done in Drupal 8? – JamesWilson Jul 23 '17 at 18:21
1

You can use Menu item visibility module to hide the item for specific user role.

This module exposes configurable and extendable visibility settings for menu links. For example, if you want to create a 'My account' link that points to /user, both anonymous and registered users have access to the /user path, so both will see the link, even if anonymous users do not technically have an account. Using this module will allow you to only have your 'My account' link visible to registered users, and hide it from anonymous users.

  • I think this module works only on the frontend side and not on the backend side(admin), but I found the solution to my problem, Thanks anyway – GwenM Nov 21 '13 at 4:31
  • It also work for admin side. Only admin user can see all menu links. If you have found, then add your answer and accept that. – Jayendra Kainthola Nov 21 '13 at 4:53
1

I found the solution: function MODULE_menu_alter(&$items) {

    //deny the menu if they are not admin (if they don't have the acess right to adsminister users)
    $items['admin/structure/menu/manage/%menu']['access callback'] = array('MODULE_check_menus_access');
    $items['admin/structure/menu/manage/%menu']['access arguments'] = array(4, 'administer menu');
    //edit
    $items['admin/structure/menu/manage/%menu/edit']['access callback'] = array('MODULE_check_menus_access');
    $items['admin/structure/menu/manage/%menu/edit']['access arguments'] = array(4, 'administer menu');
    //add
    $items['admin/structure/menu/manage/%menu/add']['access callback'] = array('MODULE_check_menus_access');
    $items['admin/structure/menu/manage/%menu/add']['access arguments'] = array(4, 'administer menu');
}

function MODULE_check_menus_access($menu, $permission){
    global $user;
    $account = $user;

    if (user_access($permission, $account)) {
        if(array_key_exists ($menu['menu_name'], $menuList)){
            return user_access('can manage '.$menu['menu_name'], $account);
        }
        else{
            return true;
        }
    }
}

/**
 * Implements hook_permission().
 */
function MODULE_permission() {
  return array(
    'can manage navigation' =>  array(
      'title' => t('Can manage navigation menu'),
      'description' => t('Limit access to the navigation menu'),
      ),
    );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.