8

I'm asking myself, where is the code that hides "My account" & "Log out" in the user menu? Just curious, to learn from that code. Any way to know where to search if you want to know something like this?

I added a "Log in" link to this menu, and I want to hide this when nobody is logged in.

3 Answers 3

13

When you have something that happens with a link, you need to look in the code for a hook_menu(). Then you need to guess which module it is in. In this case, take a look at core User module:

function user_menu() {
  $items['user/login'] = array(
    'title' => 'Log in',
    'access callback' => 'user_is_anonymous',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );

  return $items;
}

Note the 'access callback' => 'user_is_anonymous', line. This means that you can only do this if you are anonymous (ie, not logged in). This means that you should just be able to make a menu entry with "user/login" as the path, and the link will only show when you are logged out.

16

You can add a menu link to user/login, the access callback of that verifies that it is only shown to anonymous users. Just like user/logout is only visible for logged in users.

The definition of these callbacks() are, as mentioned in the other answer, in user_menu()

1
  • +1 and the same is true for user/register - create the link the usual way through the menu UI, and it only appears for users who aren't logged in automatically. Commented May 17, 2016 at 16:05
1

Check the menu definitions in user_menu() in user module. The access callback determine, when will the menu item be shown.

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.