4

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.

2
  • 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
    Commented Oct 11, 2012 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. Commented Oct 15, 2012 at 15:03

2 Answers 2

4

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.

2

try to paste this into devel/php :

dpm(module_implements('preprocess_html'));
dpm(module_implements('process_html'));

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.