Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get $node variable in html.tpl.php - Drupal 7

I'm trying to allow users to update head titles and meta descriptions for each page. I thought that an easy way to achieve this would be to add a field to the 'Basic page' content type for the page title, then check if that field is not empty in html.tpl.php and if it is not, override $head_title with this user-defined value.

However, it appears that the $node variable is not available in html.tpl.php. Can anyone suggest a way for me to make this data available in this template file, or alternatively, alter $head_title before it is sent to html.tpl.php? Thanks for reading.

like image 648
Will Avatar asked Aug 05 '11 13:08

Will


2 Answers

Taken in part from this thread that I found: http://drupal.org/node/1041768...

In your template.php, you can do the following:

function yourtheme_preprocess_html(&$variables) {
  // If on an individual node page, add the node type to body classes.
  if ($node = menu_get_object()) {
    $variables['head_title'] = $node-> // find your cck field here
  }
}
like image 128
nmc Avatar answered Oct 24 '22 04:10

nmc


Bit messy, but would work:

if(arg(0) == 'node' && !empty(arg(1))) {
  $node = node_load(arg(1));
}

However, you might prefer http://drupal.org/project/metatags_quick (an interrim module until the full http://drupal.org/project/metatags is finished).

like image 25
GSP Avatar answered Oct 24 '22 05:10

GSP