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'),
),
);
}