13

I have created several views templates that are currently stored in my active themes directory. In would prefer these templates be stored within my module directory.

My question is, how can I bring these theme files (views-view-field.tpl.php for example.) into my modules directory and have them be seen by Views?

Looking at the Views modules, I can see that default templates are stored in views/theme, but I don't see how it has been implemented.

29

This took me a while to figure out as well. The following code should be placed in your .module file (the code assumes your module is called "NAME")

/**
* Implements hook_views_api().
*
* Enabling using templates inside module
*/
function NAME_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'NAME'),
    'template path' => drupal_get_path('module', 'NAME'),
  );
}

This will tell Views to look in the root directory of your module. I personally am a fan of adding a subdirectory to the module to hold template files. To do this append the directory name to the end of the template path, eg:

'template path' => drupal_get_path('module', 'NAME') . '/templates',
  • Adding them to a sub-directory is a fantastic idea, cleans things up and it works perfectly. Future searchers make sure to clear your Drupal cache. Thank you @Reekan Mantell – Citricguy Aug 29 '12 at 11:25
  • Thank you! I've been hacking my admin theme for months just to over-ride a views field in seven. Now I don't have to. – JM01 Jan 29 '15 at 23:10
  • And I must say that you can still put TPL in your theme even if you have implemented this code. Thanks. – anou Feb 10 '16 at 16:38
  • 2
    This will not work with Features modules, instead use hook_views_api_alter() as noted here: deeson.co.uk/labs/views-templates-features-module – Aubrey Robertson Jul 29 '16 at 17:01
2

Another approach is to define the template using 'hook_theme', as this article describes: http://grayside.org/2010/09/how-override-views-field-template-module

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.