2

When a user is logged in the User menu shows 'My Account' link and the 'Logout' link. However, when logged out the menu disappears.

How do I make a menu link to the user login page that only shows for anonymous users in the User Menu?


If Trey posted his comment as an answer, I would have checked it. I have disabled the block module since I use context. I created a second sitewide context that is triggered by the anonymous user role and placed the menu block with the link for login page in the user region. It works.

I still would like to see this accomplished in code. I tried to access the variables through preprocess functions but the menu links are already rendered by that time.

7

That is a feature. An anonymous user doesn't have an account nor can he log out, why should there be links to these pages?

You might want to update your question to better explain what you actually want instead of explaining how it works right now :)

EDIT: To have a link to the Login page, just create a menu entry that points to /user/login. Just like /user/logout, this link will only be shown if you can log in (which means that you are not already logged in).

  • This is the correct answer. – Adam S May 26 '11 at 10:45
1

Check out the logintoboggan module; it provides a login block that works more as expected.

Or just go to your blocks, create a new block, and use the following code as block body:

<a href="/user">Sign In</a>

Use the following PHP code to determine the block visibility:

<?php global $user; return($user->uid == 0); ?> 
  • logintoboggan does this but I don't want to add more code if the site really doesn't need it. – Adam S May 25 '11 at 22:24
  • In order to change your login block from "my account | logout" to "sign in | register", which is what I assume you are looking for, you will have to add more code, either into your template.php file in your theme or via a modules like logintoboggan, I prefer the module in this scenario because it adds valuable functionality that becomes very useful as your site grows – Trey May 25 '11 at 22:34
0

This is not that complicated if you're trying to do this in Drupal 7 using the Bartik theme.

Create a user menu item and call it Login. Give it the link, user/login. It will automatically show up for anonymous users, and disappear for logged in users.

If you have problems with this, you may need to rebuild your permissions (on admin/reports/status page)

0

The login links should appear when a user is not logged in. If you go to http://drupal.org, and you are not logged in, you will see the login link; once you are logged in, the login link is replaced by the logout link.

In fact, the following definitions are used for the "user/login" and "user/logout" menu callbacks:

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

  $items['user/logout'] = array(
    'title' => 'Log out', 
    'access callback' => 'user_is_logged_in', 
    'page callback' => 'user_logout', 
    'weight' => 10, 
    'menu_name' => 'user-menu', 
    'file' => 'user.pages.inc',
  );

The first menu callback is not visible when the user logged in; the second menu callback is not visible when the user is not logged in.

Similar definitions are used in Drupal 6:

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

  $items['logout'] = array(
    'title' => 'Log out', 
    'access callback' => 'user_is_logged_in', 
    'page callback' => 'user_logout', 
    'weight' => 10, 
    'file' => 'user.pages.inc',
  );

In both the cases, only when the access callback returns TRUE the link is accessible to the user; when the access callback returns FALSE, the link is not visible to the user.

  • No it doesn't. Login toboggan does that. I just don't want to add that much code if I don't have to. @see drupal.org/node/937916 – Adam S May 26 '11 at 3:56
  • @Adam S No, that is how Drupal behaves. As far as I know, Drupal.org doesn't use Login Toboggan; I have never used that module in my site, and when I am logged in, the "log in" link is replaced by the user profile, and the "log out" link. – kiamlaluno Jun 2 '11 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.