6

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?

1
  • Is there any reason you haven't made a custom site module and use hook_user_view?
    – Rider_X
    Commented Sep 5, 2012 at 22:44

4 Answers 4

9

This finally worked for me; it's essentially what was posted at http://drupal.org/node/1262070#comment-5038516, but with some function renaming and generalizing:

function YOUR_MODULE_menu_alter(&$items) {
    // Change the title of user profile pages to the user's name.  Gak.
    $items['user/%user']['title callback'] = 'YOUR_MODULE_user_page_title';
}

and then

function YOUR_MODULE_user_page_title() {
    if (arg(0) == 'user') {
        // Load uid from url
        $user = user_load(arg(1));
        // Get the full user name somehow; here, I'm calling a function of my own.
        $output = get_user_full_name($user);
    }
    // Fallback to username if no fields are present
    if (empty($output)) {
        $output = $user->name;
    }
    return $output;
}

But, come on. It's nice that the hooks are there, but doing something like setting the title of a page REALLY shouldn't be this hard/obscure...

1
  • 5
    It's not that hard :) It's your requirement for a very specific dynamic title that introduces the complexity here, not Drupal. For the record there are other places you could make the same call (hook_page_alter(), hook_preprocess_page(), hook_user_view()) without having to alter the menu at all, and just use drupal_set_title()
    – Clive
    Commented Oct 8, 2012 at 17:58
1

I know this has already been answered. However, assuming you wanted to change the title to an actual name it would have been easier to use the RealName module. You could have also done this, which I think is way easier than the accepted solution:

function hook_username_alter(&$name, $account) {
  if (arg(0) == 'user' && is_numeric(arg(1))) {
    if (isset($account->uid)) {
      $name = $account->your_field_name['und'][0]['safe_value'];
    }
  }
}

You could probably even wrap $account in entity_metadata_wrapper() and use value() instead of screwing around with the array.

0

it makes more sense to me to do it with this: http://api.drupal.org/api/drupal/includes%21theme.inc/function/template_preprocess_html/7

but really, whatever floats your boat ;)

5
  • drupal_set_title doesn't appear to work when called from hook_preprocess_html. Any other thoughts? Commented Aug 1, 2012 at 21:43
  • I've updated my original post with some additional details. It would seem preprocess functions in general are not the way to go. Commented Aug 1, 2012 at 22:22
  • hm.. I've done this before, I will dig around and try to find what project it was for.. also, are you able/willing to hack something in to page.tpl.php ?
    – jdu
    Commented Aug 2, 2012 at 13:37
  • ugh... i was hoping to keep everything in a function for speed purposes, but, i speed isn't exactly important for this app, so whatever works will work for the time being Commented Aug 2, 2012 at 15:16
  • Updated my issue again, definitely check it out. Commented Aug 2, 2012 at 16:11
0

This is untested but it seems like hook_user_view is the right way to go, the caveat being you cant put this in your theme, but in a custom module.

function mymodule_user_view($account, $view_mode, $langcode) {
  if ($account->uid == 1 && $view_mode == 'full') {
     drupal_set_title('my custom title');
  }
}

Your Answer

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

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