14

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.

3 Answers 3

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',
4
  • 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
    Commented Aug 29, 2012 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
    Commented Jan 29, 2015 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
    Commented Feb 10, 2016 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 Commented Jul 29, 2016 at 17:01
3

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

3

If the above examples will not work in Drupal 8 try this:

/**
 * Implements hook_theme().
 */
function MODULENAME_theme($existing, $type, $theme, $path) {
  return [
    'views_view_grid__dashboard__page_1' => [
      'template' => 'views-view-grid--dashboard--page-1',
      'base hook' => 'views',
    ],
  ];
}

OR

/**
 * Implements hook_theme_registry_alter().
 */
function MODULENAME_theme_registry_alter(&$theme_registry) {
  // Override view template in module, not in the theme
  $theme = \Drupal::theme()->getActiveTheme()->getName();
  if ($theme == 'adminimal_theme') {
    $theme_registry['views_view_grid']['path'] = drupal_get_path('module', 'MODULENAME') . '/templates';
  }
}

But note that MODULENAME_theme_registry_alter works only once after cache clear and can not be usable in some places

views_view_grid is the template name in your templates folder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.