Drupal 7: Override Node Template for a Specific Content Type in Module

Some module may be required to provide node template for a special content type. This article is going to show how to achieve it.

Let us see how it became possible before proceeding with the actual solution. At end of function template_preprocess_node() in 'node.module' file, you can see that it add two theme hook suggestions of pattern node__<type> and node__<nid>. Which means we can create theme entry in hook_theme() implementation following those patterns. Then theme() function will take that theme entry instead of theme entry node.

We can start with implementing hook_theme() using above explained trick. Assuming our content type name is 'test'.

/**
 * Implements hook_theme()
 */
function MY_MODULE_theme() {
  return array(
    'node__test' => array(
      'render element' => 'elements',
      'template' => 'node--test',
    ),
  );
}

Then you would need to copy file 'node.tpl.php' from 'modules/node' to your module's directory and rename it to 'node--test.tpl.php'. I just followed same pattern as theme hook suggestion in naming of template file for this type of node. But you can choose any name of your choice for the context as you like.