Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
This is a very simple block to display a link to the login page when the user is not logged in and to display a logout link when the user is logged in. Create a custom block and set the input filter to php.
Drupal 4.6
global $user;
if (!$user->uid) {
// Change the following line's Login/Register text to whatever you want.
return l("Login/Register", "user/login");
} else {
// The following line will display the username you are logged in as.
return 'Logged in as ' . check_plain($user->name) . '<br><a href="/?q=logout">Logout</a>';
}
If you don't want the block to redundantly show up in the login section, add this to the custom block's path field.
<^(?!user)>
Drupal 5
This one shows the login form or a link to logout:
global $user;
if ($user->uid) {
print l("Log Out " . $user->name,"logout");
} else {
print drupal_get_form('user_login_block');
}
This shows only links:
global $user;
if ($user->uid) {
print l("Log Out " . $user->name,"logout");
} else {
print l("Log In","user");
}
Comments
Retaining destination page
If you want to get taken back to the original page you were on when logging in from a text link then replace:
print l("Log In","user");
with:
print l("Log In","user/login" .drupal_get_destination());
Works on Drupal 5.1 (probably 4.x as well?) and with thickbox login module.
Login/logout and return to current page
This variant will return the user to the current page after login/logout.
Or, if you prefer a button:
:(
This doesn't appear to be working in Drupal 6. I'd a button [no text] that changes upon user login/logout [as described above.] The code should work... but no dice.
A good idea to remove
A good idea to remove visibility using this pattern:
user/login*
Rewrite of login block to create a custom utilites menu
This is my first community contribution. I am learning to love drupal!
Great snippet just a tiny code correction...
istar100 thanks for the code. Exactly what I was looking for. Just minor correction.
Instead of:
it's:
Minor edits
I love this snippet from istar100. Here's a 2-line layout of the same, already with udig's correction.
The greeting is on the first line and the login/logout +utilities below. Useful for placing the block on narrower regions. Actually I just added a line break after the welcome greeting.
Works also on Drupal 6. Happy snippeting. ;)
D6 improvement
Edit: Thanks a ton for the code and idea, it was very useful!
I don't know about others, but the redirect to destination after login didn't work for me using the above code.
Here's how I did it, so that the alias is shown in the link if available:
If you don't care about aliases, you can change $redirect to something prettier:
And maybe turn 'alias' to FALSE (or remove it). See this for the drupal_get_destination() api.
---
Paul Craciunoiu
Improvement?
I wonder if I am missing something with the following suggestion for improvement...
Instead of:
Couldn't you just do this?
As I understand the meaning of the 'alias' parameter, it does exactly what you're trying to do by hand. The improvement would be in the part where you don't require urldecode() and substr(), which use knowledge about the implementation of drupal_get_destination(). That implementation may change in some future version.
The biggest advantage of my suggestion is that it nicely cooperates with global_redirect.
Great but It's not helping my case
Well... i have rewrited my whoe comment, because it was going realy long and has too much words for too few useful information. So:
I am using Drupal 6 and i want to force the user to login before acessing the content. i made a custom home page that redirect you depending of your status: if you are logged in, you'll go to http://dreamsgarden.toile-libre.org/index.php?q=node and if you are not, you'll go to http://dreamsgarden.toile-libre.org/index.php?q=user/login (http://toile-libre.org does not support clean urls).
The goal is to keep login page's layout (the "create a new account","login" and "i forgot my password" tabs, mainly), wich the login block does not have. The problem is that i can't deny content access to anonymous, because my custom home page will be acess-denied too, and therefore totaly useless.
I got a kind of workaround with this (i'm a newbie in php, and you will surely find a lot of improvements):
(Hey, Drupal.org messed with my php code! why is there a "title=[...]" thing added?)
And the block shoots me "unexpected T_[something] on line 2" (i guess it's the "!")
so my plan was to use something like :
<meta http-equiv="refresh" content="0; http://dreamsgarden.toile-libre.org/index.php?q=user/login&destination=$url-you-were-before-being-redirected">
Any hint??
i see that you have explained the "&destination=" and ".get[...]" but i don't know how to appy it to my case since i'm not trying to add links to a block, but instead forcing my users to log in. Everybody's help is welcomed! (and yeah, i know, my english s**ks, so please... Be nice)
I got an idea...
Finally stopped ripping my head and used the standard login block with content access denied to anonymous users. Considering using logintoboggan.
How can I remove the search
How can I remove the search link?
Whenever I remove
I lose the rest of the links as well... Having search in this, for me, is kindof unnecessary as there are several other search locations on the site that no one ever utilizes
Edit to add:
Nevermind, figured it out, you have to leave
return $user_utilities;
but can 86 the redundant search functionality by deleting the remainder of the code I posted above.
Thanks for the nice snippet!