9 November 2015

This nifty little snippet will scan your module for tpl.php files. Include template files from your module. Override default template in custom module. I usually have a single module where I keep my custom project based alterations.

Source code viewer
  1. /**
  2.  * Implements hook_theme_registry_alter().
  3.  */
  4. function HOOK_theme_registry_alter(&$theme_registry) {
  5. // Add overridden templates from the modules directory.
  6. $module_path = drupal_get_path('module', 'HOOK');
  7. // Find all .tpl.php files in this module's folder recursively.
  8. $template_file_objects = drupal_find_theme_templates($theme_registry, '.tpl.php', $module_path);
  9.  
  10. // Iterate through all found template file objects.
  11. foreach ($template_file_objects as $key => $template_file_object) {
  12. // If the template has not already been overridden by a theme.
  13. if (!isset($theme_registry[$key]['theme path']) || !preg_match('#/themes/#', $theme_registry[$key]['theme path'])) {
  14. // Alter the theme path and template elements.
  15. $theme_registry[$key]['theme path'] = $module_path;
  16. $theme_registry[$key] = array_merge($theme_registry[$key], $template_file_object);
  17. $theme_registry[$key]['type'] = 'module';
  18. }
  19. }
  20. }
Programming Language: PHP