5

I want to change the menu button on my main nav bar depending if the user is or isn't a member of a group.

I need one button to 'register' if the user isn't part of a group and that same button changes to 'add post' if he/she is.

I do not know what to put in the array. Do I put an IF statement similar to "Correct way to use hook_menu_alter"?

   function change_menu_menu_alter(&$items) {
     $items['/node/[nid]'] = array(
       'access callback' => 'change_menu_custom_menu_access_callback',
       'access arguments' => array(1),
       // **I don't know what goes here!!!**
     );
       return $items;
   }
   function change_menu_custom_menu_access_callback($gid) {
     return og_is_member('node', $gid);
   }

EDIT: I do not have gid in my url as I am using pathauto to use [node:content-type]/[node:title] on my URL which is why I don't think the current answer works on my website.

1 Answer 1

3

This is how I solved a similar problem with the hook_menu() and the use of 'title callback' and 'page callback'

function YOUR_MODULE_menu() {
    $items = array();

    $items['link'] = array(
        'title' => 'test',
        'description' => t('Just a test'),
        'title callback' => 'change_title_link',
        'title arguments' => array(1),
        'page callback' => 'go_to_place',
        'page arguments' => array(1),
        'access callback' => 'user_is_logged_in',
        'expanded' => TRUE,
        'weight' => 100,
        'menu_name' => 'main-menu',
        'options' => array('attributes' => array('class' => array('perfil-usuario'))),
        ); 

    return $items;
}

In this function you can change the link title

function change_title_link($gid) {
  if(og_is_member('node', $gid)) {
    return 'Post';
  }
  else {
    return 'Register';  
  }
}

Here you can redirect to the desire url

function go_to_place($gid) {
  if(og_is_member('node', $gid)) {
    drupal_goto('/someplace/post');
  }
  else {
    drupal_goto('/someplace/register');
  }
}
3
  • Fantastic! makes perfect sense apart from 3 questions I have: 1) What does the options' => array('attributes' => array('class' => array('perfil-usuario'))), ); mean? 2) what does array(1) mean after title arguments and page arguments? 3) Why is there first an items array() followed by an items ['links'] array? Thanks
    – user54721
    Feb 7, 2016 at 18:08
  • @ldvt5 I don't have time at this moment you can read more at api.drupal.org/api/drupal/modules!system!system.api.php/… for 1 and 2, and for 3 you can read this php.net/manual/en/language.types.array.php Feb 8, 2016 at 18:04
  • I do understand your answer better now - but (as you can see from my edit) should it still work with [node:content-type]/[node:title] in the URL?
    – user54721
    Feb 12, 2016 at 17:53

Your Answer

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