Drupal – Theme node template by module

Suppose we have a content type called Silly Question. We can theme the node template by adding the node-silly_question.tpl.php in the theme folder. But if we are working on a module, we should ask Drupal to read the .tpl.php in our module folder first because we shouldn’t ask the module user to copy the .tpl.php from our module folder to their theme folder.

What we need is to implement the hook_theme_registry_alter(). Add it in your .module file.

/**
 * Implementation of hook_theme_registry_alter()
 */
function <module>_theme_registry_alter(&$theme_registry) {
  $template = 'node';
  $originalpath = array_shift($theme_registry[$template]['theme paths']);
  $modulepath = drupal_get_path('module', '<module>');
  // Stick the original path with the module path back on top
  array_unshift($theme_registry[$template]['theme paths'], $originalpath, $modulepath);
}

 

Replace the <module> with your module name and now Drupal should read the node-silly_question.tpl.php in your module folder first instead of the theme folder.

Done =)

Reference: Drupal 6 – Custom Node Type Template in Module

2 thoughts on “Drupal – Theme node template by module”

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.