Views templates in a features module
Note, these code examples are for Drupal 7.
You might find it desirable to place views template files into a module rather than the site theme. If the module is a feature which contains a view and is something that you might want to use on another site, then it makes sense to group the view and it's templates in the same module. The following article describes how to tell views that your module contains views template files and how to tell features that you want to tell views that the module contains views template files.
If your module includes a views-templates directory, you can inform views that the folder contains views templates by using the hook_views_api function:
/** * Implements hook_views_api(). * We are specifying this here ourself as we want to include a views * template directory. */ function mymodule_views_api() { return array( "version" => "3.0", 'template path' => drupal_get_path('module', 'mymodule') . '/views-templates', ); }
However, if you are using the features module to export the view itself you can't change hook_views_api as it is auto generated by features and is placed in the mymodule.features.inc file. If you edit it then any changes will be removed next time you update the features module.
Edit! - There is a hook to alter the views api implementation directly now without any further workarounds. In you module file, add any additional views_api elements using the pattern below.
/** * Implements hook_views_api_alter(). * * Views api is specified by features but we want to add the views-template directory to it. */ function mymodule_views_api_alter(&$apis) { if (!empty($apis['mymodule']) && $apis['mymodule']['version'] == '3.0') { $apis['mymodule']['template path'] = drupal_get_path('module', 'mymodule') . '/views-templates'; } }
However, it is possible with a little tinkering to get all the benefits of features and still be able to specify the hook_views_api function yourself. If you implement the following hook_features_export_alter function in your features module .module file then you can tell features not to include the hook_views_api. This means you can include it yourself in you module file as above.
/** * Implements hook_features_export_alter(). * * Alter the final export array just prior to the rendering of * defaults. * FORBID the hook_views_api, we are going to add that ourself. * * @param array &$export * By reference. An array of all components to be exported with a given * feature. * @param array $module_name * The name of the feature module to be generated. */ function mymodule_features_export_alter(&$export, $module_name) { if ($module_name == 'mymodule' && !empty($export['features']['ctools']['views:views_default:3.0'])) { unset($export['features']['ctools']['views:views_default:3.0']); } }
If your module file now has the hook_views_api and hook_features_export_alter functions you can remove hook_views_api from the mymodule.features.inc file and flush your caches. Next time you update the features module it will not include the hook_views_api and any views templates you put in the views-templates directory of your module will get included.