1

I have built a module that adds a new custom content type. I need to use my own html.tpl.php for that content type, and the html template sits on my module folder.

I have done somee xperiments, I was able to replace the template of the node content, but it's not enough, and I can't seem to find some examples or explanatory documentation about it.

If my module is called "tomato" and my custom type is called "sauce", and I already have a file called html--sauce.tpl.php on my module folder, how do I tell Drupal to use that template when a "sauce" node is loaded?

  • 5
    Possible duplicate of Override html.tpl.php per node type – Shawn Conn Apr 16 '16 at 15:52
  • It's not a duplicate, the other one is for a theme, I need it for a module, I tried the theme road, it does not work on module. – Terix Apr 16 '16 at 18:39
  • 1
    I guess it wasn't obvious from the other question that hook_preprocess_html() can be used in module's. Answer added. – Shawn Conn Apr 16 '16 at 19:22
2

Modules can use hook_preprocess_html() to override theme templates. You need 3 things:

Using a module called MYMODULE, with a new node type mynodetype, & a template in the module dir named templates/html--mynodetype.tpl.php, we can add the following hooks to MYMODULE.

/**
 * Implements hook_preprocess_html().
 */
function MYMODULE_preprocess_html(&$vars) {
  $node = menu_get_object();

  // Add a new theme suggestion when viewing node's of mynodetype.
  if ($node && $node->nid && $node->type == 'mynodetype') {
    $vars['theme_hook_suggestions'][] = 'html__mynodetype';
  }
}

/**
 * Implements hook_theme().
 */
function MYMODULE_theme($existing, $type, $theme, $path) {
  $theme = array();

  //Add our templates/html--mynodetype.tpl.php to the theme registry
  $theme['html__mynodetype'] = array(
    'template' => 'html--mynodetype',
    'path' => drupal_get_path('module', 'MYMODULE') . '/templates',
  );
  return $theme;
}

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.