Theming Drupal Usernames

Posted by emmajane on October 10, 2008 - 12:02am in

I'm working on a new project right now where I had to create a bunch of user accounts on behalf of users. To keep things really easy for (what I think might be) a non-technical crowd I decided to use email addresses for usernames. Great! Except I don't really want to show email addresses all over the Web site where people names ought to be. The function theme_username() fixed up the "submitted by" text, but I wanted more than that. I wanted to change the navigation block and the user profile page as well. Here's how I did it in Drupal 6.

Caveat: usernames in Drupal have to be unique. With this "hack" multiple users can be identified by the same display name. This might not be appropriate for your site. Use this snippet with great caution and understanding that not all Laura Thomases and Christopher Moloneys are created equally.

Configuration

  1. Install the profile module that comes with Drupal core.
  2. Go to admin/user/profile to configure the user profiles.
  3. To the profile, add a "single-line textfield" for "display name." (I actually used Given Names, and Family Name to allow me to sort by first or last name later if I want to.) Remember the machine-readable name. Make this field visible in the registration form, and make the values "required."
  4. Add a "list selection" that will allow people to choose if they want to show their "username" or "display name." Remember the machine-readable name.
  5. In the permissions section, allow users to change their own usernames.

Theme
Once you have the configuration set up, you can begin altering the displayed name in your theme. There are three functions I had to create and/or modify in my template.php file: THEMENAME_preprocess_page, THEMENAME_preprocess_block, THEMENAME_username. In each of these functions I did a little test to see if the display name toggle was set to show "username" or "display name." I also quickly checked to see if there was an @ character in the username (with the assumption that an @ symbol meant the account holder hadn't logged in and changed their username). Although I did have the name swap in THEMENAME_preprocess_user_profile as well, it doesn't appear to be necessary when each of _page, _block and _username are all themed.

THEMENAME_preprocess_page: Fixes the name that appears on the user's profile page (display title and "head" title which appears at the very top of the browser window).
// Map the First and Last name onto the username if requested,
// or if the username appears to be an email address.

$user = user_load($vars['user']->uid);
if ( ($user->profile_displayname == "Given names" || strpos("@", $vars['user']->name) > 0)
&& $vars['title'] == $user->name):
$name = $user->profile_first . " " . $user->profile_last;
$vars['head_title'] = str_replace($vars['title'], $name, $vars['head_title']);
$vars['title'] = $name;
endif;

THEMENAME_preprocess_block: Fixes the navigation block to show the display name instead of the username
// You don't really need to test all blocks, just those related to the 'user' module
$block = $vars['block'];

    if ($block->module == "user"):
$user = user_load($vars['user']->uid);
if ($block->subject == $user->name) :
$block->subject = $user->profile_first . " " . $user->profile_last;
endif;
endif;

THEMENAME_username: Fixes the name in the "submitted by" text in nodes.
// Copy the exactly the whole function from the API documentation
// paste it exactly into your theme's template.php file
// and add the following to the BEGINNING of the function

$user = user_load($object->uid);
if ($user->profile_displayname == "Given names" || strpos("@", $user->name) > 0 ):
$object->name = $user->profile_first . " " . $user->profile_last;
endif;

(Note: this works in Drupal 6.x, it will not work in previous versions of Drupal.)

Comments

Leave your feedback at the bottom. Comments may be held in moderation.

sun (not verified) had a comment on October 10, 2008 - 6:37pm

Display users, user names, and user pictures differently? With arbitrary decorators, like post counts or comment counts?

Well, that's what http://drupal.org/project/user_display is for.

Core worthy. But unfortunately still misses an UI module.

So, give it a try. And if you want to work on a UI, just get involved! :)

Cheers,
Daniel

AjK (not verified) had a comment on October 10, 2008 - 9:05pm

http://drupal.org/project/authorship

Wrote it ages ago and now maintained by lafo

No D6 version but it's not rocket science to upgrade it.

Post new comment
The content of this field is kept private and will not be shown publicly.
  • 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> <span> <img> <blockquote> <b> <i>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.