0

I am building a module that requires me to get the $head_title variable in the hook_node_view for some processing...I have found info about that variable in relation to themes and around core, but not getting it in a module...I have successfully used drupal_get_title(), and variable_get() to get info, but I can not figure out how to get $head_title...

Any help?

0
0

$head_title is one of the variables passed to the html.tpl.php template file, and set from template_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);

That variable would be available as $variables['head_title'], if your module implements hook_preprocess_html() or hook_process_html(). Otherwise, you just need to execute similar code to the one used by template_preprocess_html(). To adapt that code for a module, outside hook_preprocess_html() and hook_process_html(), I would rewrite it as follows.

  // 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', ''));
    }
  }

  // Remove the following line if you just need the string.
  $head_title_array = $head_title;

  $head_title = implode(' | ', $head_title);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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