Posted on November 7, 2006 | Tags:
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;
}