HOWTO: Smart Login Menu Hook
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;
}













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! :)
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
fixed in 5
we fixed this in drupal 5 … still a good tip
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
Post new comment