Chapter Three LLC

HOWTO: Smart Login Menu Hook

Matt Cheney

It is a neat property of Drupal that you can define a menu item pointing at “logout” that, because of its permissioning, only shows up to users who are logged-in (and in need of logging-out). This same trick, sadly, does not work for login because “user/login” is accessible to both logged-out users (as the login page) and to logged-in users (as their profile page) - so it shows up all the time which can be sort of confusing.

One solution is to employ the following menu hook to create the location “login”. Then you can set up two menu items (Login pointing to “login”, Logout pointing to “logout”) which will smartly switch placement depending on the logged-in/logged-out status of the user. Just throw the following code in a module and away you go:

function smartlogin_menu($may_cache) {
  $items = array();
  global $user;
  if ($may_cache) {
  $items[] = array(
    'path' => 'login',
    'title' => t('Login'),
    'callback' => 'drupal_goto',
    'callback arguments' => array('user/login'),
    'access' => ($user->uid == 0),
    'type' => MENU_CALLBACK);
  }
  return $items;
}

Drupal 6

And how would this be done in drupal 6 is something I’d like to know…

Posted by kibble (not verified) | Dec. 1st, 2007 @ 12:29pm | Link to this Comment

silly question

Hi, thanks for sharing this. Just one silly question; how does the smartlogin_menu ever get called? As my first foray into menu hooks, putting this into my module did, well, nothing! :)

Posted by Jeff (not verified) | Nov. 8th, 2006 @ 5:00pm | Link to this Comment

Your module is named?

This assumes you’re using a module called smartlogin.module. The hook_menu command is one of drupal’s core means for including functionality. Whatever your custom module is called, modulename_menu will fire when drupal is building its menu system, so you’ll need to name the function accordingly.

More information here:

http://api.drupal.org/api/4.7/function/hook_menu

Posted by Josh Koenig | Nov. 9th, 2006 @ 10:43am | Link to this Comment

fixed in 5

we fixed this in drupal 5 … still a good tip

Posted by moshe Weitzman (not verified) | Nov. 8th, 2006 @ 1:36pm | Link to this Comment

would not be a patch to logintoboggan?

it adresses a good point, I wonder it would be good idea to submit to logintobpggan.module at Drupal

regards

Posted by Anonymous (not verified) | Nov. 8th, 2006 @ 4:55am | Link to this Comment

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br/> <br /> <p> <img> <blockquote> <i> <b> <u>
  • Lines and paragraphs break automatically.
  • SmartyPants will translate ASCII punctuation characters into “smart” typographic punctuation HTML entities.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options