2

Normally the /user/uid page shows the username as page-title. I'm using the Profile2 module to leverage more fields to my users, including a 'full name' field which I'd also like to use as the page title.

How would I go about this?

1 Answer 1

4

If you actually seek to always use the full name for the user's Profile2 when his/her username is displayed, then you should implements hook_username_alter(). Any well behaving module (contrib and core) should use it.

function MODULE_username_alter(&$name, $account) {
  // Get the user profile
  $profile = profile2_load_by_user($account, 'profile_type');
  if ($profile) {
    $fullnames = field_get_items('profile2', $profile, 'field_fullname');
    if ($fullnames) {
      $name = $fullnames[0]['value'];
    }
  }
}

If you only want to override the username when used as title of the user page, you can alter the menu entries for user and user/%user to change their title callback to a custom one.

function MODULE_menu_alter(&$items) {
  $items['user']['title callback'] = $items['user/%user']['title callback'] = 'MODULE_user_menu_title';
}

function MODULE_user_menu_title($account) {
  if (is_object($account)) { // Updated here
    $profile = profile2_load_by_user($account, 'profile_type');
    if ($profile) {
      $fullnames = field_get_items('profile2', $profile, 'field_fullname');
      if ($fullnames) {
        return $fullnames[0]['value'];
      }
    }
    return user_menu_title($account);
  }
  return '';
}
3
  • Does it work as expected? Should it go in template.php or in a tpl.php file? Aug 14, 2012 at 9:56
  • These are hook implementations, unless documented otherwise hook implementations go in modules, not themes. So not in template.php or any .tpl.php file. Aug 15, 2012 at 14:01
  • Doesn't work on head title. Is it just me or it is the case for everyone?
    – qasimzee
    Jul 4, 2015 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.