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?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityIf 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 '';
}