How to Override a Views Field Template from a Module
Some months ago I wanted the solution to overriding a Views Field template entirely from within a module. I spent hours trawling documentation and issues, and playing with a little trial and error. In the end, I came up with a solution:
/**
* Implementation of hook_theme().
*/
function custom_module_theme($existing) {
return array(
'views_view_field__view_name__field_name' => array(
'arguments' => array('view' => NULL, 'field' => NULL, 'row' => NULL),
'template' => 'views-view-field--view-name--field-name',
'original hook' => 'views_view_field',
'path' => drupal_get_path('module', 'custom_module') . '/theme',
),
);
}
By defining a Views template in this way, you are doing the equivalent to building your template and dropping it into your Theme’s directory. This is sufficient for Views to notice the existence of the template from within your module. At that point the standard Views logic of finding the most specific template available for each field comes into play. In this case, every instance of field name in view name will use this template… unless, of course, I have a more specific template somewhere that targets that field in a specific display.
Note that the theme key and template name are the same, but for the swapping of hyphens and underscores. The way both of those are named is critical, else Views will not properly notice the template. All the options listed in Theme Information in the Views UI is available for use here.
Note that this is very derivative of the hard work of others, and I will link to it here when I come across it again.
views template overrides in module directory was one of my key references. Since I checked that thread, the marvelous @dereine posted a patch with the intent to guide Views to find theme templates in module directories as well. It Needs Review, so if the code above bores you, go review the patch!