17

In my template.php file for my theme I have tried the following:

function media_preprocess_page(&$vars, $hook) {
  if (isset($vars['node'])) 
  {
      // If the node type is "blog" the template suggestion will be "html--blog.tpl.php".
       $vars['theme_hook_suggestions'][] = 'html__'.$vars['node']->type;

      // If the node type is "blog" the template suggestion will be "page--blog.tpl.php".
       $vars['theme_hook_suggestions'][] = 'page__'.$vars['node']->type;

      // If the node id is "33" the template suggestion will be "page--33.tpl.php".
       $vars['theme_hook_suggestions'][] = 'page__'.$vars['node']->nid;    
  }

    //Create page suggestion for first part of url-alias
    $url_alias = drupal_get_path_alias($_GET['q']);
    $parts = explode('/', $url_alias);

    $vars['theme_hook_suggestions'][] = 'page__'.$parts[0].'__alias';  
}

This works for page--nodetype.tpl.php, but not for html--nodetype.tpl.php

You may be asking why do you need to override the html.tpl.php template per node type. It is because there is markup that I don't want to include for this particular node.

28

The name of a preprocess function is based on the theme/template that is being processed. To preprocess the html.tpl.php file you'll need to use hook_preprocess_html():

function media_preprocess_html(&$vars) {
  $node = menu_get_object();

  if ($node && $node->nid) {
    $vars['theme_hook_suggestions'][] = 'html__' . $node->type;
  }
}
3

@Clive approach is very smart.

Also do note when in html.tpl.php file, you can read the content type you are dealing with from $variables['classes'], which will give you something like html not-front not-logged-in no-sidebars page-node page-node- page-node-5638 node-type-CONTENT-TYPE-NAME

With that, you can change how html.tpl.php file is behaving with this:

if (strpos($variables['classes'],'node-type-YOUR-CONTENT-TYPE') == true ) {
  echo 'Do something special  for YOUR-CONTENT-TYPE ';
}

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.