Avoid Drupal theme file cluttering

16 Apr 2010

A Drupal theme may consist of several template overrides. These files are usually stored in the themes root folder. For a simple theme, this is not a problem but if you are coding a complex theme with lots of overrides of views and cck templates it starts to get pretty cluttered in your themes directory.

While working with Development Seeds excelent base theme Tao I came across an elegant solution to this. Using hook_theme() we can tell Drupal that we want to store our custom template files in another directory in our themes folder.

In your themes template.php put the following function:

function MYTHEME_theme($existing, $type, $theme, $path) { $template_path = drupal_get_path('theme', 'MYTHEME') .'/templates'; return drupal_find_theme_templates($existing, '.tpl.php', $template_path); }

Substitute /templates with the directory name of your choice.