0

Any idea on how I can render the menu tab item into a menu group?

My goal is to add a user tab in navigation menu group. I tried hook_menu but I cant manage to make it work.

Here's my approach:

function lms_core_menu() {
  global $user;

  $items = array();
  $items["user/$user->uid/myresults"] = array(
    'title' => 'My Results',
    'type' => MENU_NORMAL_ITEM,
    'menu_name' => 'navigation',
  );

  return $items;
}

Ideas will be greatly appreciated.

EDIT: See the image attached.

enter image description here

3 Answers 3

1

Just change

'type' => MENU_NORMAL_ITEM

to

'type' => MENU_LOCAL_TASK

and clear the caches.

The MENU_LOCAL_TASK option will make the menu item render as a tab in the user/% group. If you want to control the tab order you can use the 'weight' key in hook_menu().

4
  • Sorry for being not so clear Clive. I mean I want a specific link in the tab to be in the Navigation menu group also. Commented Jan 3, 2013 at 13:47
  • Ah sorry, my fault :) I don't think that's possible using hook_menu() - the type has to be MENU_NORMAL_ITEM for an item to be automatically added to a menu, and it has to be MENU_LOCAL_TASK to get it into a tab group. Since you can't have both I think you're stuck choosing one or the other. Personally I'd go with MENU_LOCAL_TASK and just add the link to the navigation menu manually, even though it's not ideal
    – Clive
    Commented Jan 3, 2013 at 13:53
  • Yes its already in MENU_LOCAL_TASK. But I dont think I can do it manualy because there are almost 1.5k users in the site. Commented Jan 3, 2013 at 13:56
  • 1
    It's a shame you're using Drupal 6, there's a Menu Token module for D7 that would be perfect for this. There's some code on this post on d.o that might be helpful, you could add a standard link to the navigation menu and preprocess it to add the currently logged in user's id
    – Clive
    Commented Jan 3, 2013 at 14:03
0

I'm a little confused here so, first off a couple things in some comments below:

function lms_core_menu() {
  global $user;  // will be that of your admin when s/he rebuilds the menu...

  $items = array();
  $items["user/$user->uid/myresults"] = array( // so this will be hardcoded most probably as user/1/myresults
    'title' => 'My Results',
    'type' => MENU_NORMAL_ITEM,
    'menu_name' => 'navigation',
    // where is your page callback, eg, is it defined somewhere else and you are hoping to just piggyback on it in here?
  );

  return $items;
}

because I would imagine you could do something along the lines of:

function lms_core_menu() {

   $items=array();

   $items['lms_core/myresults'] = array(
      'title' => 'My Results',
      'type' => MENU_NORMAL_ITEM,
      'menu_name' => 'navigation',
      'access callback' => 'user_is_logged_in',
      'page callback' => 'lms_core_myresults',
   );

   return $items;

}

function lms_core_myresults() {
   global $user;

   $account=user_load($user->uid);

   return FUNCTIONTHATCREATESMYRESULTSBASEDONTHEUSER($account);
}

where that big capitalized function above is the page callback defined wherever

$items['user/%user/myresults']

is defined.

0

You can, using code similar to the following one.

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

  $items['user/myresults'] = array(
    'title' => 'My Results',
    'type' => MENU_NORMAL_ITEM,
    'menu_name' => 'navigation',
    'access callback' => 'user_is_logged_in',
    'page callback' => 'lms_core_myresults_page',
  );

  return $items;
}

function lms_core_myresults_page() {
  global $user;

  // Use $user for the user account information.
}

If you want to show the menu item as menu tab, and as menu link, you can use code similar to the following one.

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

  $items['user/myresults'] = array(
    'title' => 'My Results',
    'menu_name' => 'navigation',
    'access callback' => 'user_is_logged_in',
    'page callback' => 'lms_core_myresults_page',
  );

  $items['user/%user/myresults'] = array(
    'title' => 'My Results',
    'type' => MENU_LOCAL_TASK,
    // The default access callback is user_access(), when the 
    // access arguments are an array containing the permission users must have
    // to see the menu item.
    'access arguments' => array('the permission to see this tab'),
    'page callback' => 'lms_core_myresults_tab',
    'page arguments' => array(1),
  );

  return $items;
}

function lms_core_myresults_page() {
  global $user;

  // Use $user for the user account information.
}

function lms_core_myresults_tab($account) {
  // Use $account for the user account for which the tab is shown.
}

The difference is that the menu link will be visible only to the currently logged-in users for their own account, while the tab will be visible to every user with the right permission, for the user account %user. (This means users could see the tab for their own account, or for the accounts of other users, depending on the permission.)

Instead of the default access callback, you can use a custom one, as in the following code.

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

  $items['user/%user/myresults'] = array(
    'title' => 'My Results',
    'type' => MENU_LOCAL_TASK,
    // Instead of the default access callback, the code uses a custom one. 
    // The argument passed to the access callback is the user object matching the
    // user ID passed in the URL.
    'access callback' => 'lms_core_user_access',
    'access arguments' => array(1),
    'page callback' => 'lms_core_myresults_tab',
    'page arguments' => array(1),
  );

  return $items;
}

function lms_core_myresults_tab($account) {
  // Use $account for the user account for which the tab is shown.
}

function lms_core_user_access($account) {
  if ($account->uid == $GLOBALS['user']->uid) {
    // The currently logged-in user is watching the tab for her/his own account.
    return user_access('permission for the users who watch their own account');
  }
  else {
    return user_access('permission for the users who are watching the account of another user');
  }
}

In this example, the access callback checks a different permission, if the user is watching the tab for another user account, or her/his own user account.

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.