How to display fullname in Drupal instead of username using template.php



Following function in template.php does the trick:

 function phptemplate_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;
    }
    $profile = user_load(array('uid' => $object->uid));
    if ($profile->profile_name_first." ".$profile->profile_name_last) {
      if (drupal_strlen($profile->profile_name_first." ".$profile->profile_name_last) > 20) {
        $name = drupal_substr($profile->profile_name_first." ".$profile->profile_name_last, 0, 15) .'...';
      }
      else {
        $name = $profile->profile_name_first." ".$profile->profile_name_last;
      }
    }
 
    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);
    }
    else {
      $output = check_plain($object->name);
    }
 
    $output .= ' ('. t('not verified') .')';
  }
  else {
    $output = variable_get('anonymous', t('Anonymous'));
  }
 
  return $output;
}

 

 


COMMENTS


I was trying to output the Real Name (Fullname) instead of Username in user's account page. I tried this function for my theme, but it didn't work. I come through that the function phptemplate_username doesn't work in zen theme, simply there is no such function. So, this is my little trick I've made at template.php

function tarrot_preprocess_page(&$vars, $hook) {
		if(arg(0) == 'user') {
			if (!arg(1)) {
				global $user;
			}
			if (arg(1) && !arg(2)) {
                $url = $_SERVER['REQUEST_URI'];
                $username = substr($url, strrpos($url, '/') + 1);
                $user = user_load(array('name' => $username));
			}
			$profile = content_profile_load('Profile', $user->uid); 
			$fullname=$profile->field_fname[0]['value'].' '.$profile->field_lname[0]['value'];
			$vars['title']=$fullname;
		}
}

P.S. This function gets content (variables) from Content Profile. Hope it will help you.

after testing it I figured out that this should me much more easier

function tarrot_preprocess_page(&$vars, $hook) {
		if(arg(0) == 'user') {
			$profile = content_profile_load('Profile', arg(1)); 
			$fullname=$profile->field_fname[0]['value'].' '.$profile->field_lname[0]['value'];
			$vars['title']=$fullname;
		}
}

Explanation: when you go to profile page (account) of any user
the first argument - arg(0) == 'user'
and the second one - arg(1) == uid

Its very simple =)

function YOUR_THEME_NAME_preprocess_page(&$vars, $hook) {
if(arg(0) == 'user') {
$profile = content_profile_load('Profile', arg(1));
$fullname=$profile->field_fname[0]['value'].' '.$profile->field_lname[0]['value'];
$vars['title']=$fullname;
}
}