Theming the drupal username

If you look a drupal node, drupal displays the author of this node as the username of the user or anonymous if the author is not registered user. Now if we want to display the real name of the user (a field we will add in the profile module) instead of the username, drupal provides a theming function that let's you modify the output of the username before it is printed on a node. Let's see how we can do this.

Creating a full name field in the profile module.

We'll start by enabling the profile module under http://example.com/admin/build/modules.

Next we'll create a custom profile in the profile module interface which is found under http://example.com/admin/user/profile.

To add a new field click on the single-line textfield link under the add new field section. 

Fill in the fields as above. (note the from name value = profile_full_name). Save the field.

Now we need to add a full name for our user profile. Go to your user account. Under the profile tab, enter a value for your full name.

Save the form.

Theming the username

Drupal provides a preprocess function to format the output of the username for a page. It's called theme_username.

The theme_username function takes the $object variable as parameter, do some operations and it and return the output as $output. It also checks if the page viewer has persmission to access the author profile. It yes it displays the username as a link.

Here's the theme_function from the drupal 6 api :

{syntaxhighlighter brush: php;fontsize: 100; first-line: 1; }function theme_username($object){

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

// Shorten the name when it is too long or it will break many tables.
if (drupal_strlen($object->name) > 20) {
$name = drupal_substr($object->name, 0, 15) .'...';
}
else {
$name = $object->name;
}

if (user_access('access user profiles')) {
$output = l($name, 'user/'. $object->uid, array('attributes' => 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 (!empty($object->homepage)) {
$output = l($object->name, $object->homepage, array('attributes' => array('rel' => 'nofollow')));
}
else {
$output = check_plain($object->name);
}

$output .= ' ('. t('not verified') .')';
}
else {
$output = check_plain(variable_get('anonymous', t('Anonymous')));
}

return $output;

}{/syntaxhighlighter}

To theme the output of the username, we'll create a new function in our template.php file. The function will be similar to the theme_username function. Copy the code above and paste in template.php. rename the function to yourthemename_username(). Next we'll add these lines :

{syntaxhighlighter brush: php;fontsize: 100; first-line: 1; }profile_load_profile($object);
if(!empty($object->profile_full_name)){
$object->name = $object->profile_full_name;
}{/syntaxhighlighter}The above lines load the profile into the $object variable, checks if it is not empty and then assign the $object->name the value of the profile field defined above. (here profile_full_name is the name given to our field when we created the full name textfield).

We will place these lines inside the if statement where the function checks for a uid and a name (line 3).

The final code will look like this :

{syntaxhighlighter brush: php;fontsize: 100; first-line: 1; }function garland_username($object){

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

profile_load_profile($object);
if(!empty($object->profile_full_name)){
$object->name = $object->profile_full_name;
}

// Shorten the name when it is too long or it will break many tables.
if (drupal_strlen($object->name) > 20) {
$name = drupal_substr($object->name, 0, 15) .'...';
}
else {
$name = $object->name;
}

if (user_access('access user profiles')) {
$output = l($name, 'user/'. $object->uid, array('attributes' => 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 (!empty($object->homepage)) {
$output = l($object->name, $object->homepage, array('attributes' => array('rel' => 'nofollow')));
}
else {
$output = check_plain($object->name);
}

$output .= ' ('. t('not verified') .')';
}
else {
$output = check_plain(variable_get('anonymous', t('Anonymous')));
}

return $output;

}{/syntaxhighlighter}

If you load any page now, you will still see the username being used on pages. This is because drupal does not know of this function yet. We need to clear the theme registry. Go to performance (admin/settings/performance) and clear the cache.

Now when you load a page you should see the fulll name of the user instead of the drupal username.

Comments

Right on! It's super easy to do if you follow the instructions here.

Thanks dude

Thanks for the tutorial.

This is perfect, thank you! The issue I am having is that I would like to do this for every instance of the username on the site. Is this possible? I've been looking everywhere to try and figure this out. Let me know.

If you are using the hook_username function like we did above. (yours will be yourthemename_username()) and you place it in your template.php, it should work every time Drupal calls for the username.

I tried this and it works fine to all my nodes. But when I add comments to these nodes - the comment won't be saved. Any idea?

-Sheena

Thanks for sharing, Now I know how to theming the drupal username. Germanium

Add new comment