1

I know you can change menu links depending on drupal based roles (authenticated, non authenticated...) and by using Rules module also.

I am using Organic Group roles and I am having difficulties in showing different links on the menu bar depending whether my site's user IS or ISN'T a member of a group.

When he/she signs up a 'join' button is on the nav bar --> after joining a group he can now write content so the 'add' button replaces the join button on the nav bar with the appropriate URL paths.

Can a hook be used to do this?

0

You can do this using the access callback of the items array in hook_menu_alter. The implementation of the user menu gives a good example. To make it work with Organic groups, you could declare an access callback function using og_is_member or og_user_access:

function MY_MODULE_menu_alter(&$items) {
  $items['MY/PATH'] = array(
    'access callback' => 'MY_MODULE_custom_menu_access_callback',
    // Assuming your group is a node and the menu appears on the nodes
    // page (/node/[nid]), then array(1) would pass the node ID to your
    // function, which in this case acts as a group ID.
    'access arguments' => array(1),
    // etc...
  );
    return $items;
}
function MY_MODULE_custom_menu_access_callback($gid) {
  return og_is_member('node', $gid);
}

Where MY/PATH would be the path to the entity type representing the group. If you are using a node as a group, this would be node/%. As 'access arguments' we use the arg function. 0 would be node and 1 would be the node ID of the node.

  • My group is created from a 'create group' content type. My questions are: 1. under 'title' what do I put? -> I want to use this hook on the default menu so do I just put 'Main Menu'? 2. What goes under 'MY/PATH'? 3. Under 'access arguments' array do I add the URL of the menu items I want to change? This is my very first hook so a more detailed explanation would be much appreciated! EDIT: Just to let you know I have created a module and added a .info and a .module file that will contain this hook – qaz123 Jan 31 '16 at 19:02
  • @qaz123 Updated the answer. – user5950 Feb 1 '16 at 0:56
  • UPDATE: I have just realised I have got [node:content-type]/[node:title] in my Pathauto Settings so no gid is present in the URL so that's why it might not work... – qaz123 Feb 2 '16 at 16:16
  • @qaz123 Should work with path alias as well, as Drupal knows the internals path. So if it's a node, use node/%, it does not matter if you have an alias. – user5950 Feb 3 '16 at 5:19
  • Sorry for this bit I'm not good at programming and I'm finding this hard... This is what I understand: The menu alter function looks at items in my url path - to do this it calls the result from menu access callback function to get the group id from the node url. Under access arguments do I have to list the arguments/ what to run the user is part of a group (TRUE: show the add content button) and if the user hasnt created a group (FALSE show add group button) Is this correct? (Is my case similar to this: tinyurl.com/zylyckc) – qaz123 Feb 3 '16 at 15:49

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.