Tuesday 8 January 2013

Tweaking a Drupal 7 user profile page

Sometimes (ok, often) doing something programmatically in Drupal 7 is like drawing blood from a stone. I wanted to replace the username with the user's real name as the title on all user profile pages. After much Googling (other search engines are available), I worked out how to do it.

Add the following to your chosen theme's template.php file - and assuming you've created an additional field in your user records called "field_profile_realname"...
function [yourthemname]_preprocess_page(&$variables) {
  $arg = arg();
  if ($arg[0] == 'user' && is_numeric($arg[1]) && empty($arg[2])) {
    # we're looking at a user profile page
    $user_data = user_load($arg[1]);
    if (isset($user_data->field_profile_realname['und']['0']['value'])) {
      $variables['title'] = $user_data->field_profile_realname['und']['0']['value'];
    }
  }
}