Drupal 4.6.5

I have configured Drupal to display a single block to users - the login block. Once a user has logged in, the login block disappears. What I would like to do is continue to display that block but replace the username and password text boxes with some text such as "Logged in as: johnsmith" and a "Logout" button.

I found the following code in another topic:

<?php
  global $user;
  if (!$user->uid) {
    // Change the following line's text to whatever you want.
    print l("Login/Register", "user/login");
  }
  elseif ($user->uid) {
    // The following line will display the username you are logged in as.
    print 'Logged in as ' . check_plain($user->name) . '<br>' . l("Logout", "logout");
  }  
?>

The contents of the elseif section is basically what I want to display in the login block. Where would I need to add this code and would any additional code need to be modified to ensure the block displays after logging in?

Your advice regarding this would be most welcome.

Comments

nevets’s picture

You just need to goto administer -> blocks, then click 'add block' tab. Place the code in block body and make sure you select 'PHP code' for the input format.

stretchwickster’s picture

But I don't want to replace the login block, I just want to continue displaying it after a user has logged in but replace the Username and Password text boxes with "Logged in as..." text and a logout button.

nevets’s picture

Without modifing core (not recommended) I don't think you can do that. Which is why you find variations of the login block in various postings.

stretchwickster’s picture

Also, I would like to retain the "Create new account" and "Request new password" links.

Any ideas anyone? I'm sure there must be a fairly straightforward way of using the existing login block and replacing its contents once a user is logged in?

nevets’s picture

Re: 'Also, I would like to retain the "Create new account" and "Request new password" links.', those links only make sense for someone not logged in and if I recall correctly at least one produces an error if you try and use it while logged in.

stretchwickster’s picture

nevets,

Thanks for all the comments. Your original reply did the trick in the end. I couldn't see how it was going to work when you first suggested it.

Basically, I created a custom block and inserted the following php code into it:

<?php
  global $user;
  if ($user->uid) {
    // The following line will display the username you are logged in as.
    print '<div id="logged-in-text">Logged in as:</div><div id="logged-in-user">' . check_plain($user->name) . '</div><div id="logout-button">' . l("Logout", "logout") . '</div>';
  }
?>

I didn't realise, but the custom block only displays if a $user->uid exists. So, before logging in, the conventional login block is displayed (with the username and password text boxes, the "Create new account", "Request new password" and "log in" button all visible) and because uid doesn't exist, the custom block is not shown.

After logging in, the conventional login block disappears and the custom block is displayed with the name of the currently logged in user and a logout link. I have the blocks styled the same, so that it looks like one and the same block that has been updated.