Real names instead of usernames in Drupal

One issue we’ve run across using drupal for our website is that it assumes you are creating a community of anonymous/pseudonymous users. Drupal doesn’t have built-in support for a ‘display name’ or ‘full name’ or ‘real name’ (like “John Smith”) that is distinct from the username (like “jsmith”).

Here is some code you can add to the end of your template.php file in your drupal theme to display the first and last names of site users instead of usernames. It assumes you have added two extra profile fields called profile_firstname and profile_lastname. This username override also incorporates some other changes to phptemplate_username already made in drupal 6 (rel nofollow for links, and strike out the ‘unverified user’ notice).

//replace the first line with the name of your theme,
//or else name the function phptemplate_username
function CHANGETOYOURTHEMENAME_username($object) {



  if ($object->uid && $object->name) {

   

    $thisuser = user_load(array(uid=>$object->uid));

    $fullname = $thisuser->profile_firstname.' '.

                               $thisuser->profile_lastname;



    // Shorten the name when it is too long or it will break many tables.

    if (drupal_strlen($fullname) > 20) {

      $name = drupal_substr($fullname, 0, 15) .'...';

    }

    else {

      $name = $fullname;

    }



    if (user_access('access user profiles')) {

      $output = l($name, 'user/'. $object->uid, array('title' => t('View my profile.')));

    }

    else {

      $output = check_plain($name);

    }

  }

  else if ($object->name) {

    // Sometimes modules display content composed by people who are

    // not registered members of the site (e.g. mailing list or news

    // aggregator modules). This clause enables modules to display

    // the true author of the content.

    if ($object->homepage) {

      //$output = l($object->name, $object->homepage);

      $output = l($object->name, $object->homepage, array('rel' => 'nofollow'));

    }

    else {

      $output = check_plain($object->name);

    }

    //$output .= ' ('. t('not verified') .')';

  }

  else {

    $output = variable_get('anonymous', t('Anonymous'));

  }

  return $output;

}

If you are using the bio module instead of the profile module, replace the $fullname line above with something like this:

    if ($nid = bio_for_user($object->uid)) {
      $fullname = db_result(db_query("SELECT title FROM {node} WHERE nid = %d", $nid));
    }

The login/username is still displayed all over blogs though. To change that you need to modify the blog module directly. Here is a version of the modules/blog/blog.module file (blog.module.tar.gz) that fixes most instances. Again though this version assumes 2 profile fields for first name and last name.

Vote on this feature request if you want this fixed in drupal core. That feature request is already a year and a half old with over 200 comments on it.

About these ads
Posted in drupal, opensource
5 comments on “Real names instead of usernames in Drupal
  1. Doug Holton says:

    Actually I found out you can have usernames with spaces in them, for example “Real Name” instead of realname.So that makes these hacks unnecessary.It may require the logintoboggan module, which is useful also because you can let people login using their email address instead of their username.

  2. Kassissieh says:

    Unless you are using an external authentication source such as LDAP. Then such a feature is very necessary.

  3. Kassissieh says:

    Also try the Authorship module: http://drupal.org/project/authorship

  4. Doug Holton says:

    We’re trying to use LDAP too.We found a module called ‘alternative login’ that let’s them have 2 login names, one that is the same as their LDAP name (no spaces), and one that is like a real name.http://drupal.org/project/alt_login

  5. Christian Grauer says:

    Thanks for this patch! I really couldn't belive that drupal lacks such a basic thing!@doug holton: what if two users have the same real name? Usernames are unique, so are alternative usernames with alt_login! -> not unnecessary at all.

Comments are closed.

Doug Holton

Doug Holton

developing educational technology

Archives

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 3,526 other followers

Follow

Get every new post delivered to your Inbox.

Join 3,526 other followers

%d bloggers like this: