0

I am using Organic Groups and I have used this post to override the roles and permissions of OG Groups.

I need a hook to alter a button on my main menu that changes depending on whether the user is a member of a group (has any role in that group) or isn't a member of a group (and therefore hasn't got a role associated with his username)

The default button that always shows is 'add' when a user is associated with a group that button needs to be changed to 'remove' - with the according URL

I have created the following using this Drupal page:

function my_module_menu() {

    $items['link'] = array(
        'title callback' => 'change_link',
        'title arguments' => array(1),
        'page callback' => 'go_to’,
        'page arguments' => array(1),
        'access callback' => 'user_is_logged_in',
        'menu_name' => 'main-menu',
        ); 

    return $items;
}
function change_link($gid) {
  if(og_is_member('node', $gid)) {
    return 'Remove';
  }
  else {
    return 'Add';  
  }
}
function go_to($gid) {
  if(og_is_member('node', $gid)) {
    drupal_goto('www.mydomainurl.com/remove');
  }
  else {
    drupal_goto('www.mydomainurl.com/add');
  }
}

As well as fixing the above code - what does the array(1) do in page and title call back? --> I just copied them as is from the Drupal API page... (Does it look for the 'gid' in the last 2 functions?

UPDATE: (if any user of the site are part of any group - regardless of role - they will see only the 'remove' button if they aren't part of any group they will see the 'add' button.)

1
  • You want to add a link or you want to alter a link ? As I see you are implementing hook_menu instead of hook_menu_alter. And that link has to do with OG's (un) subscribe link or not ? As for the array(1) search in the hook_menu's page for Callback Arguments it has a really good explanation.
    – mchar
    Commented Feb 19, 2016 at 9:41

1 Answer 1

0

Here is a sample code close enough to your question. It may need some modifications to its parts in order to meet your needs, but more or less you got the idea.

/**
 * Implements hook_menu_alter().
 */
function foo_menu_alter(&$items) {
  $items['alter/%gid/path']['access callback'] = 'og_user_role_callback';
  $items['alter/%gid/path']['access arguments'] = array(1);
}

/**
 * Determine if logged-in user has access to menu or not.
 */
function og_user_role_callback($gid) {

  // Get OG role of the user. Could be 'member', 'non-member' or 'Group administrator'.
  global $user;
  $group_type = 'node';
  $uid = $user->uid;
  $member = og_get_user_roles($group_type, $gid, $uid, $include = TRUE);

  switch ($member) {
    case 'member':
      return TRUE;
    case 'non-member':
      return FALSE;
    case 'Group administrator':
      return TRUE;
  }
}
1
  • Unfortunately it doesn't work... Just to be sure I have added the 'add' button URL to the top $items variable and the 'remove' button url on the 2nd $items variable.(Both links currently show on the nav bar)- is that correct? I have only got 'member' in my group roles as they all have the same permissions so I got rid of 'non member' and 'Group administrator' - I have updated the question (see bold)
    – qaz123
    Commented Mar 1, 2016 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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