1

I am trying to programmatically disable menu based on account settings/user taxonomy term field, is there a better way to do it? do I need to use hook menu? to disable already built menu, as I have a lot of users in department that needs to get filtered to only show the menu they can edit

UPDATE :

I wanted to hook the permisssion of user so not all can edit the menu structure, like edit links and so on, like example I allowed permissions manage menu 1, manage menu 2, for role a, then I wanted to hook that menu 1 is only available for user with term reference 1. something like that

thank you!

4
  • Do you mean you want to have menu access control by the value of user object? – Jimmy Ko Jul 6 '16 at 17:47
  • And is your menu defined by hook_menu() function? – Jimmy Ko Jul 6 '16 at 17:49
  • @JimmyKo menu is created via the drupal admin system "add menu" and I wanted menu access control based on user taxonomy reference field instead of user role, is there anyway to achieve that? – Boby Jul 6 '16 at 17:58
  • Could you edit your question to describe more details? I don't know what kind of settings you want to have for user account as well. Drupal core did provide user role for granting different scope of permissions. If the menu access is limited by user role, does it fit your requirement? Or Do you need more granular control to restrict the access by user individually? – Jimmy Ko Jul 6 '16 at 23:01
3

The answer will depend on what your created menu items route to (hook_menu() is for defining new menus so it won't help you here).

Generically, hook_menu_alter() allows you to alter the access callback so that you can override access with your own logic. If the path you're overriding is specific to a special module route (e.g. admin/dashboard), then it would make sense to use this hook.

/**
 * Implements hook_menu_alter().
 */
function MYMODULE_menu_alter(&$items) {
  $items['admin/dashboard']['access callback'] = 'MYMODULE_access';
}

/**
 * Access callback function
 */
function MYMODULE_access() {
  global $user;
  $user_entity = user_load($user->uid);

  // @TODO: Check your user entity's taxonomy field for access and return
  // TRUE | FALSE

  // EXAMPLE: Allow access for USER #123
  // return ($user_entity->uid == 123);
}

If your created menu items link to content nodes then you should look at hook_node_access(). This hook is specifically designed for controlling access to nodes so you'll have access to the node/user object from the hook's arguments.

/**
 * Implements hook_node_access().
 */
function MYMODULE_node_access($node, $op, $account) {
  $user_entity = user_load($account->uid);

  // @TODO: Check your user entity's taxonomy field for access and return
  // NODE_ACCESS_ALLOW | NODE_ACCESS_DENY | NODE_ACCESS_IGNORE
  // depending on your logic
}
5
  • I wanted to hook the permisssion of user so not all can edit the menu structure, like edit links and so on, like example I allowed permissions manage menu 1, manage menu 2, for role a, then I wanted to hook that menu 1 is only available for user with term reference 1. something like that – Boby Jul 12 '16 at 8:41
  • You should reword your question to make your requirements clearer. Right now it's not clear what else is required in terms of user permissions or roles or if the answer can elaborate further on that requirement. – Shawn Conn Jul 14 '16 at 5:40
  • I need to hook disable a menu for user with id "123" thats it – Boby Jul 15 '16 at 7:34
  • Hi @shawn, the following code works but I need now to remove the unset($items['admin/structure/menu/manage/menu-menu-test']); but instead its unsetting all of the menu instead of particular menu that I want, anyway of solving this one? as your code is already helping out! – Boby Jul 15 '16 at 13:03
  • I can't answer a question when the details of what you're asking change. All I can tell you is unset($items['admin/structure/menu/manage/menu-menu-test']); a bad practice; it will wipe that menu item out for everyone. Instead, change/add access callback to point a callback function that will return TRUE/FALSE based on your requirement (see MYMODULE_access example). – Shawn Conn Jul 16 '16 at 2:58

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.