User login

How to display different links in a Drupal page to logged in vs anonymous in users

This is how documentation gets written...

Dan Hak (3:22:01 PM): so how do you make them situational?
Dan Hak (3:22:02 PM): php?
Dan Hak (3:23:05 PM): i put the anon links in
Dan Hak (3:23:08 PM): btw
I3IVIIVI (3:23:36 PM): <?php if ($user->uid==0) { ?><a href="/user/register">create an account</a><?php } else { ?><strong><a href="/node/add/define_news" title="post an element of your definition of news">help define news</a></strong> or <strong><a href="/node/add/source" title="submit your preferred sources of truth">recommend a news source</a></strong><?php } ?>
I3IVIIVI (3:23:39 PM): oop
I3IVIIVI (3:23:49 PM): I was still editing that, but did the enter instead of a break
I3IVIIVI (3:27:05 PM): <?php if ($user->uid==0) { ?>
<a href="/user/register">create an account (and with redirect, do something)</a> or <a href="/user">log in (and do something)</a>
<?php } else { ?>
<a href="/node/add/something">do something</a>
<?php } ?>
I3IVIIVI (3:27:38 PM): but actually, it should be all php and l() function links, to see if that respects internationalization
I3IVIIVI (3:28:13 PM): if I remember it at least does translation, and it must keep track of "en" and "es" language prefixes to paths also (I hope)
I3IVIIVI (3:28:16 PM): so:
I3IVIIVI (3:28:41 PM): <?php
if ($user->uid==0) {
?>
I3IVIIVI (3:29:07 PM): darn auto tab, I better try writing this in a text document
Dan Hak (3:29:20 PM): do it
I3IVIIVI (3:30:00 PM): http://api.drupal.org/api/function/l/5
I3IVIIVI (3:30:09 PM): tell me if i guessed that url right
I3IVIIVI (3:30:15 PM): and if l does do translations...
I3IVIIVI (3:33:12 PM): <?php
if ($user->uid==0) {
l('Register', 'user/register');
} else {
l('Do it', 'node/add/doit');
}
?>
I3IVIIVI (3:33:18 PM): should be the general idea
Dan Hak (3:33:34 PM): ok
Dan Hak (3:33:48 PM): get the dropdowns working
Dan Hak (3:33:52 PM): bbu!
I3IVIIVI (3:34:17 PM): the drupal is easier than html
I3IVIIVI (3:34:22 PM): but it does need t()
I3IVIIVI (3:34:52 PM): <?php
if ($user->uid==0) {
l(t('Register'), 'user/register');
} else {
l(t('Do it'), 'node/add/doit');
}
?>

Later...

Dan Hak (4:39:04 PM): that php didnt seem to work
Dan Hak (4:39:15 PM): I tried with and without the t(
Dan Hak (4:39:17 PM): and uh
I3IVIIVI (4:39:18 PM): is it in a node? gimme the url
I3IVIIVI (4:39:28 PM): i never write code that works the first time
I3IVIIVI (4:40:11 PM): it's like in that Chinese story, where the artist had work that could come alive if he put on a final brush stroke, or completed a picture of a bird with an eye?
I3IVIIVI (4:40:17 PM): I'm like that, but with semicolons
Dan Hak (4:40:18 PM): http://dev.wsf2008.net/eng/home
I3IVIIVI (4:40:56 PM): the answers are all in the l() function, which I had gotten the right URL to btw
Dan Hak (4:40:57 PM): i probably did it wrong
I3IVIIVI (4:41:19 PM): maybe but my version is more poetic
Dan Hak (4:41:37 PM): yea, i forgot to tell you
I3IVIIVI (4:42:55 PM): oh, try adding a print statement
I3IVIIVI (4:42:59 PM): that can help...
Dan Hak (4:43:03 PM): HAHA
Dan Hak (4:43:08 PM): maybe a little

Resolution

Final result (plus global $user, which is needed as noted in the comments if not previously pulled from the global scope).

<?php
 
global $user;
if (
$user->uid==0) {
  print
l(t('Register'), 'user/register');
} else {
  print
l(t('Do it'), 'node/add/doit');
}
?>
Searched words: 
HTML a tag conditional link display drupal l() function translate

Comments

Don't Forget to Register Globals!

This won't work unless you register Globals. E.G. for a Login / Logout Button:

<?php
    global $user;
    if ($user->uid==0)
       {print l(t('Log In'), 'user');}
    else
       {print l(t('Log Out'), 'logout');}
?>

--
Bram Moreinis
845-750-6204
Principal, Game Face Web Design
http://www.gamefacewebdesign.com
Director, Hudson Valley Tech Scouts
http://hvscouts.com

"The future is already here - it's just not evenly distributed." - William Gibson

Thanks, that's right!

Edited the original post to do it right.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.