15
Sep
2010
Grayside

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!

9 comments

Adrian, Wed, 09/15/2010 - 17:16

what is the oa_feeds ?

a views name ?

Grayside, Fri, 09/17/2010 - 15:49

A typo in extracting the code from the module in which I used it. Something I will be more publicly releasing soon for you OpenAtrium-using RSS notifications junkies.

Hi,
In order to override the views field provided by the theme information link in the Views UI interface. You have to modify the default php coding jfor details about that visit drupal.org.You have to store your templates are to be stored in your theme folder. Move your templates to mymodule/templates because module associated with the template is self-contained and theme-independent. Then edit the ‘theme_paths’ attribute of views_view_field in hook_theme_registry .

dereine, Wed, 09/15/2010 - 22:56

Alternative you could use http://drupal.org/node/627378 and no hook_theme

Grayside, Fri, 09/17/2010 - 15:50

Awesome! And that’s one of my reference issues.

mongolito404, Wed, 09/15/2010 - 23:02

You can also use a theme function instead of a template file. Views itslef uses its theme_views_view_field() function if not template override is provided. If you don’t set the ‘template’ property, Views wil use a theme function instead of a template.

function MODULE_theme($existing, $type, $theme, $path) {
  return array(
    'views_view_field__view_name__field_name' => array( 
        'arguments' => array('view' => FALSE, 'field' => FALSE, 'row' => FALSE),
        'original hook' => 'views_view_field',
  ),
}
 
function theme_views_view_field__view_name__field_name($view, $field, $row) {
  return '<span class="foo">'. $view->field[$field->options['id']]->advanced_render($row) .'</span>';
}

Matthew Slater, Thu, 09/16/2010 - 01:58

Although it’s not strictly perfectly accurate, because you are assuming the phptemplate theming engine, but this is simpler because you’re not declaring a new theme callback.

function phptemplate_views_view_field__view_name__field_name($view, $field, $row) {
 print_r($field);
 print_r($row);
}

Grayside, Fri, 09/17/2010 - 15:54

I have no problem assuming a phptemplate-based theme. But I avoid the phptemplate namespace anyway–it’s sloppy, and there’s always the chance someone else uses it for the same purpose. By not using it myself, I don’t have that potential problem.

Sean Larkin, Thu, 09/16/2010 - 08:40

But could you have posted just 3 days earlier when I spent 2 hours trying to figure this out? ;)

Really helpful. -s