Overriding the theme_username function
stevenandwes - June 25, 2009 - 14:23
Hi,
I'm trying to override the theme_username function in order to display a users first and last name instead of their username. I have two custom fields on the Profile Information page called "profile_first" and "profile_last" which requires all users to enter a first and last name. Pretty much other than that all I've done is copied the function from theme.inc into my template.php file and renamed the function phptheme_username.
Now I'm stuck here. Any further help would be awesome.
Thanks
-Steve

_
You may wish to checkout the http://drupal.org/project/realname module which does exactly this. I use it and it works great.
_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.
RealName
Yeah my boss wants me to hard code it. I suggested that as well.
_
Then use the code. ;-)
_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.
What do you mean?
What do you mean?
_
The module does exactly what you're asking for-- take the _username code from there.
_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.
...
So I copied the _username code from the realname module and pasted it into my template.php file in place of the old _username code, and nothing happened... any help would be awesome.
_
Sorry-- I didn't mean to imply it was that simple or you should grab the whole function-- I meant to look to that code for what you needed to add to your theme_username override.
Something like the following should do it:
<?php
function phptemplate_username($object) {
if ($object->uid && $object->name) {
$fullname = $object->profile_firstname . " " . $object->profile_lastname;
// Shorten the name when it is too long or it will break many tables.
if (drupal_strlen($fullname) > 20) {
$name = drupal_substr($object->name, 0, 15) .'...';
}
else {
$name = $fullname;
}
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;
}
?>
_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.