2
votes

I am trying to replace the title of the user profile page, which is the username, with other fields that were added later. Trying to show the full name as the title of the page.

I edited page.tpl.php $title with following code but it doesn't work.

if(arg(0)=="user") 
{
   $title=$field_user_last_name;
}
4

4 Answers

2
votes

Install the RealName module and go to /admin/config/people/realname to change the pattern used for the user's name.

I'm using Profile2 to add the first and last name field and the following pattern:

[user:profile-person:field_first_name] [user:profile-person:field_last_name]

But you can probably add the fields at /admin/config/people/accounts/fields without using Profile2 aswell.

2
votes

A cleaner way to do this:

function mymodule_page_alter() {  

  global $user;  

  if(drupal_get_title() === $user->name) {  
    drupal_set_title('New value');  
  }  
}
2
votes

You can accomplish this functionality nicely using the Entity API module's metadata wrapper and hook_user_view(). Add a dependency to Entity API in your module's .info file and then the code could look something like the following (assuming you want to set the title to the user's full name and you have fields called field_first_name and field_last_name):

/**
 * Implements hook_user_view().
 */
function MYMODULE_user_view($account, $view_mode, $langcode) {
  // Set the page title of the user profile page to the user's full name.
  $wrapper = entity_metadata_wrapper('user', $account);
  $first_name = $wrapper->field_first_name->value();
  $last_name = $wrapper->field_last_name->value();
  if ($first_name && $last_name) {
    drupal_set_title($first_name . ' ' . $last_name);
  }
}
0
votes

I got solution!!!

I added following code on preprocess_page function on my template.php

  if(isset($variables['page']['content']['system_main']['field_name'])){
      $title=$variables['page']['content']['system_main']['field_name']['#object']->field_name['und'][0]['value'];
      drupal_set_title($title);
 }