Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I wanted to add some data to the page head title and created a template_preprocess_html function in my module.

I added a couple entries to the head_title_array and imploded the array back into head_title.

At first it looked like it wasnt working at all, but then I checked and in html.tpl.php the $head_title_array was updated.

My temp solution is to implode the array there before printing it but that's a little hackish for me.

Thoughts on where the title is being set or re-set to prevent it from being changed in template_preprocess_html?

Module code is aproximately:

function MODULE_preprocess_html(&$variables) {
  $head_title = $variables['head_title_array'];
  array_unshift($head_title, 'TEST');

  $variables['head_title_array'] = $head_title;
  $variables['head_title'] = implode(' | ', $head_title);
}

and in html.tpl.php:

<title><?php print implode(' | ', $head_title_array); ?></title>

I just noticed that the metadata module is storing variables in an array called metatag_set_preprocess_variable. That might be the cause, I'll investigate further.

share|improve this question
    
How were you setting the page head title? Seems like you should be calling drupal_add_html_head() in your hook_preprocess_html() and setting the title tag in there. –  jmarkel Oct 11 '12 at 20:53
    
Not sure about in general, but in the Zen template the head tag is printed in html.tpl.php so I probably shouldn't set it directly. That's a new function for me though that will be useful for some other metadata. –  Eric Goodwin Oct 15 '12 at 15:03

2 Answers 2

up vote 2 down vote accepted

If you find that $head_title is different from implode(' | ', $head_title_array), it means there is a module running after yours that implements hook_preprocess_html() which runs after your implementation, or hook_process_html().

The code executed from Drupal is the following:

  • template_preprocess_html() (It runs before hook_preprocess_html().)

    // Construct page title.
    if (drupal_get_title()) {
      $head_title = array(
        'title' => strip_tags(drupal_get_title()), 
        'name' => check_plain(variable_get('site_name', 'Drupal')),
      );
    }
    else {
      $head_title = array('name' => check_plain(variable_get('site_name', 'Drupal')));
      if (variable_get('site_slogan', '')) {
        $head_title['slogan'] = filter_xss_admin(variable_get('site_slogan', ''));
      }
    }
    $variables['head_title_array'] = $head_title;
    $variables['head_title'] = implode(' | ', $head_title);
    
    // Populate the page template suggestions.
    if ($suggestions = theme_get_suggestions(arg(), 'html')) {
      $variables['theme_hook_suggestions'] = $suggestions;
    }
    
  • template_process_html() (It runs after hook_preprocess_html(), but before hook_process_html().)

    There is no code that alters $variables['head_title_array'], or $variables['head_title'].

The fact $head_title_array contains the value you expect makes me think there is really a module that implements hook_preprocess_html to alter $variables['head_title'].

To be sure your module is the last one running hook_preprocess_html(), you need to make your module weight higher.

share|improve this answer

try to paste this into devel/php :

dpm(module_implements('preprocess_html'));
dpm(module_implements('process_html'));
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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