How can I use a custom module to override views-view-table.tpl.php? I understand how to do it for a theme, but I want to provide the template using a custom module. I've already created my replacement table theme file. I've put it in a folder called "templates" inside my custom module. Now I just need to understand what hook(s) I should use to tell views to check my module for replacement templates.
2 Answers
Add a module path to the Drupal 7 theme registry by implementing hook_theme_registry_alter().
Here's an example - replace the usual with your own data :)
/**
* Implements hook_theme_registry_alter().
*/
function MYMODULE_theme_registry_alter(&$theme_registry) {
// Get module path
$path = drupal_get_path('module', MYMODULE);
// Find all .tpl.php files in this module's folder recursively.
$template_files = drupal_find_theme_templates($theme_registry, '.tpl.php', $path);
// Itterate through all found template file objects.
foreach ($template_files as $key => $template_file) {
// If the template has not already been overridden by a theme.
if (!preg_match('#/themes/#', $theme_registry[$key]['theme path'])) {
// Alter the theme path and template elements.
$theme_registry[$key]['theme path'] = $path;
$theme_registry[$key] = array_merge($theme_registry[$key], $template_file);
}
}
}
Try hook_views_api().
function YOUR_MODULE_views_api() {
return array(
'api' => 3,
// Place your exported views here if you have any.
'path' => drupal_get_path('module', 'YOUR_MODULE') . '/includes/views',
// Place your template files here.
'template path' => drupal_get_path('module', 'YOUR_MODULE') . '/templates',
);
}