At DrupalCampNYC I saw that 5.0 allows really cool login/logout menu items that recognize whether a user is logged in or not and displays the appropriate setting accordingly. I would love to have this, but I'm not that good at PHP and I need to keep 4.7.x for the gmap and location modules.

Can anyone provide a solution? What I want in particular is a primary link that says "login" and links to the login page when the user is anonymous, but says "logout" when the user is authenticated.

I thought a good solution was to have certain menu items appear according to PHP rules, but as far as I can tell, I can only apply PHP rules to menu blocks, not to the items within those menu blocks. Please help.

Comments

vm’s picture

The logout menu item can easily be placed in the primary links. This will only show to authenticated users, thus it will only show when a user is indeed logged in. Adminster -> Menus -> logout -> edit

As far as getting the login up there and making is disappear after a user is logged in, I have no idea.

billtron’s picture

Yeah, I discovered that logout does exactly that. Wonderful!

Does anyone else know why login can't be made to do the same thing? How can I elegantly provide a link to a login page without using the login block or having the link stick around once people need not login anymore?

drawk’s picture

In your page.tpl.php or wherever it is that you are placing the link, surround it in something like this:

  global $user;

  if (!$user->uid) { // anonymous user
    <a href="/user/login">log in</a>;
  }

That will only display the link if the individual viewing the page hasn't logged in.

---
www.whatwoulddrupaldo.org

billtron’s picture

I don't really know PHP, but that didn't work for me.

This, however, did:

<?php
  global $user;

  if (!$user->uid): ?> 
  <a href="/user/login">log in</a> |
<?php endif; ?>
drawk’s picture

Ah, yeah, I meant

 print '<a href="/user/login">log in</a>'

Glad you got it working.

---
www.whatwoulddrupaldo.org

billtron’s picture

Thanks for your help! I changed it back to what you meant, since it seems cleaner than what the monkeys in my basement happened to type out on their keyboards for me.

Christefano-oldaccount’s picture

There's a discussion going on about this at http://drupal.org/node/120197

Heine wrote a code snippet at http://drupal.org/node/14223#comment-105408 and it was turned into the Login Menu module maintained by rwholheb.