Skip to content

Instantly share code, notes, and snippets.

@justcaldwell
Created May 31, 2012 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justcaldwell/2843432 to your computer and use it in GitHub Desktop.
Save justcaldwell/2843432 to your computer and use it in GitHub Desktop.
An implementation of theme_username() shared at the 2012 UT Austin DrupalCamp - Theming 101 Session. Not sure of the original source. This should be pasted into your template.php file.
/**
* Implementation of theme_username()
* Change theme_username below to your_theme_name_username
*/
function theme_username($object, $link = TRUE) {
if ( !$object->profile_first_name && !$object->profile_last_name ) {
if ( $object->uid && function_exists('profile_load_profile') ) {
profile_load_profile($object);
}
}
if ( $object->profile_first_name && $object->profile_last_name ) {
$name = $object->profile_first_name . ' ' . $object->profile_last_name;
if ( $link && user_access('access user profiles')) {
return l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
return check_plain($name);
}
}
// Profile field not set, default to standard behaviour
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 ( $link && 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/**
* Implementation of theme_username()
*/
function theme_username($object, $link = TRUE) {
if ( !$object->profile_first_name && !$object->profile_last_name ) {
if ( $object->uid && function_exists('profile_load_profile') ) {
profile_load_profile($object);
}
}
if ( $object->profile_first_name && $object->profile_last_name ) {
$name = $object->profile_first_name . ' ' . $object->profile_last_name;
if ( $link && user_access('access user profiles')) {
return l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
}
else {
return check_plain($name);
}
}
// Profile field not set, default to standard behaviour
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 ( $link && 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);
}
else {
$output = check_plain($object->name);
}
$output .= ' ('. t('not verified') .')';
}
else {
$output = variable_get('anonymous', 'Anonymous');
}
return $output;
}
// 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);
}
else {
$output = check_plain($object->name);
}
$output .= ' ('. t('not verified') .')';
}
else {
$output = variable_get('anonymous', 'Anonymous');
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment