Right now I have the following function implemented in template.php
:
function ccom_page_alter(&$page) {
if (array_key_exists('#account',$page['content']['system_main'])) {
$account = $page['content']['system_main']['#account'];
if (isset($account)) {
drupal_set_title($account->field_complete_name['und'][0]['safe_value']);
}
}
}
Is this the best way to set the title on a user page? Or should I be doing this through a different hook?
EDIT: I've updated my template.php
to use hook_preprocess_user_profile
instead, and I have this:
function ccom_preprocess_user_profile(&$variables) {
drupal_set_title($variables['elements']['#account']->field_complete_name[LANGUAGE_NONE][0]['safe_value']);
}
The page title changes (like the one the browser shows you on a tab), but the user profile Drupal page title remains the username. Very annoying. Still working...
EDIT 2: I ran an experiment. I did the following:
function ccom_preprocess(&$variables, $hook) {
dpm($hook);
}
Which gave the following output when viewing a user profile page:
- search_block_form
- block
- region
- block
- region
- user_profile
- user_profile_item
- user_profile_category
- block
- region
- html
- region
- user_picture
- page
So that's the order in which the preprocess hooks are called on that page. I then created two functions:
function ccom_preprocess_user_profile(&$variables) {
dpm($variables);
drupal_set_title('mytitle');
}
function ccom_preprocess_page(&$variables) {
dpm($variables);
drupal_set_title('mytitle');
}
I first loaded the page with the ccom_preprocess_page
function commented out. The title in the browser tab changed to mytitle, but the page title stayed as the username for the user profile page being viewed.
I then commented out ccom_preprocess_user_profile
and uncommented ccom_preprocess_page
and reloaded the page again. Both the browser tab title and the page title changed to mytitle.
This tells me that somewhere after the user_profile preprocess hook function and up to and including the page preprocess hook, the page title is reset. Where that is I'm not entirely sure.
So, my thinking at this point would be to create a global variable and set it equal to the user account object available in the user_profile hook, and then in the page preprocess hook just set the title based on the complete name field available in the account object. Thoughts?