Create an alias instead of user username

Last modified: September 19, 2007 - 13:36

Note from the moderator: Thank you for the changes. You probably forgot the output in the else if ($object->name) clause; it still lacks output checks. Best use l() for the link to the homepage and check_plain for the $object->name. Thank you for your work.

This code was taken from the forum which I believe was also taken from somewhere else. I have made some fixes that allow it to work with comments and forums. Please claim responsibility if the original code was yours.

This snippet allows you to display user friendly alias names on posts and comments etc. This comes in useful if your using the Webserver_auth module and don't want to see webserver usernames on your site. It allows the users to set an alias in their profile which is then displayed on the site. If the profile field isn't set then it will use the users username.

You will need to enable the profile module and add a single line text field to your users profiles. You can call it anything you want but you will need to alter the code as appropriate. For this example I called my field 'profile_fullname'.

In your template.php file you will need

<?php
function phptemplate_username($object) {
  if (
$object->in_preview) {
      return
theme_username($object);
  }
  return
_phptemplate_callback('username', array('object' => $object));
}
?>

Create a file called 'username.tpl.php' with these contents

<?php
if ($object->uid && $object->name) {
 
// If the user has a full name defined, use that
 
$account = user_load(array(uid => $object->uid));
 
$profilename = $object->name;
  if (!empty(
$account->profile_fullname)) {
   
$profilename = $account->profile_fullname;
  }
 
// Shorten the name when it is too long or it will break many tables.
 
if (drupal_strlen($profilename) > 20) {
   
$name = drupal_substr($profilename, 0, 15) .'...';
  }
  else {
   
$name = $profilename;
  }
  if (
user_access('access user profiles')) {
   
$output = l($name, 'user/'. $object->uid, array('title' => t('View user 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, array('title' => t('View link')));
  }
  else {
   
$output = check_plain($object->name);
  }
 
$output .= ' ('. t('not verified') .')';
}
else {
 
$output = variable_get('anonymous', 'Anonymous');
}
print
$output;
?>

username is still displayed in the user profile?

seth97 - April 7, 2007 - 10:28

This works good! Excellent to have the full name displayed instead of e.g. “mad_max55”!

There is one problem: the alias is still displayed in the user profile in the upper left corner (to the left of “view”).
This means that whenever a user is posting something the full name is displayed, but if you click on his user profile his alias is shown at the top.....

Any ideas how to change the alias for the full name in the user profile?

Thanks!

changing a page's title

ashtonium - June 30, 2007 - 22:10

You can change a page title with the drupal_set_title function.

A good place for it in this instance would be in a customized version of the theme_user_profile function, placed in your template.php file:

<?php
/**
* An implementation of theme_user_profile
*/
function phptemplate_user_profile($account, $fields) {
 
// change the profile page's title to the specified profile field
  // but only if we are actualy viewing a profile page and if the specified profile field has a value
 
if ((arg(0) == 'user') && $account->profile_fullname) {
   
drupal_set_title($account->profile_fullname);
  }
 
 
$output = '<div class="profile">';
 
$output .= theme('user_picture', $account);
  foreach (
$fields as $category => $items) {
    if (
strlen($category) > 0) {
     
$output .= '<h2 class="title">'. $category .'</h2>';
    }
   
$output .= '<dl>';
    foreach (
$items as $item) {
      if (isset(
$item['title'])) {
       
$output .= '<dt class="'. $item['class'] .'">'. $item['title'] .'</dt>';
      }
     
$output .= '<dd class="'. $item['class'] .'">'. $item['value'] .'</dd>';
    }
   
$output .= '</dl>';
  }
 
$output .= '</div>';

  return
$output;
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.