Theming your views in your module on Drupal 7

This snippet is only a cut&paste form the advanced help section of Views module, i use as a reference just in case i couldn't have access to the advanced help module for any reasons and mainly because i ever forgot how to do this simple thing :-)

Theming your views in your module
You can theme these views in the module and not need to rely on the theme to do this at all; and in fact, the theme can continue to override these just like it ordinarily would, even if your module provides a theme. This is very useful for distributing a module where the view needs to look "just so."

To do this, you need to implement hook_theme() in your module:

  1. function mymodule_theme($existing) {
  2. return array(
  3. 'views_view__viewname__displayid' => array (
  4. 'arguments' => array('view' => NULL),
  5. 'template' => 'views-view--viewname--displayid',
  6. 'base hook' => 'views_view',
  7. 'path' => drupal_get_path('module', 'mymodule'),
  8. ),
  9. );
  10. }

There are a small number of gotchas in doing this that you must be aware of.

  1. When referring to a template filename, you always use dashes in the name. i.e, views-view--viewname--displayid.tpl.php. However, when referring to the hook or function names, you use underscores instead of dashes. i.e, views_view and views_view__viewname__displayid
  2. The 'arguments' change based upon which of the 3 types you're overriding. There's the 'display', the 'style' and the 'row' style. The above code is assuming the display, which is usually just views_view. Here are the possibilities:

    1. display: array('view' => NULL),
    2. style: array('view' => NULL, 'options' => NULL, 'rows' => NULL, 'title' => NULL),
    3. row style: array('view' => NULL, 'options' => NULL, 'row' => NULL),

    Be sure to use the right arguments line or the theme system will not properly translate.

  3. The 'template' line should never include the extension, so drop the .tpl.php from it.
  4. You need to make sure that the Views preprocess functions get registered. The 'base hook' line in the definition does that, but it can only do it if it comes after the Views registration, which actually happens very late in theme building. 99% of the time, your module will come before Views. You have two choices to deal with this:
    1. Set your module's weight to 11 or higher in the database. Views' weight is 10. You can make this happen automatically when the module is first installed by creating a mymodule.install file and using this code:
      1. function mymodule_install() {
      2. db_query("UPDATE {system} SET weight = 11 WHERE name = 'mymodule'");
      3. }

      If you use this method, the base hook should be set to the name of the original template being used. i.e, if this is a variate of views-view-list.tpl.php, this should be 'views_view_list'.
    2. You can also just force it to list the preprocessors without actually having to detect them. This doesn't require modifying your module's weight, which is not always possible, you can insert this code into the array:
      1. 'preprocess functions' => array(
      2. 'template_preprocess',
      3. 'template_preprocess_views_view',
      4. 'mymodule_preprocess_views_view__viewname_displayid',
      5. ),

      The first one is the global 'template_preprocess' function which all templates utilize. It does some basic things such as setting up $zebra and a few other items. See for specifics.

      The second one is the plugin specific preprocess. Like 'base hook' it should conform to the name used by the original template. i.e, if the original template was views-view-list.tpl.php then that preprocess function would be named template_preprocess_views_view_list.

      The third one is your module's preprocess function, if it needs one. In general, you probably will not need one, and you should only attempt to use one if you are reasonably familiar with the concept of preprocess functions and Drupal's theme system in general. See Drupal's theme documentation for more information.

  5. If you leave the path blank the template file will be searched for in "./" which is the Drupal install base path.

Alternative technique, starting from Views 3.x (d7):

  1. /**
  2.  * Implementation of hook_views_api().
  3.  * Info: <a href="<br />
  4. " title="<br />
  5. "><br />
  6. </a> */
  7. function hook_views_api() {
  8. return array(
  9. 'api' => 3,
  10. 'template path' => drupal_get_path('module', 'my_module') . '/templates',
  11. );
  12. }

If you are using Features module, you have to implement in this way (because hook_views_api() is already implemented by Features module and can't be overriden:

  1. /**
  2.  * Implements of hook_views_api_alter()
  3.  *
  4.  */
  5. function hook_views_api_alter(&$list) {
  6. $list['views']['template path'] = drupal_get_path('module', 'my_module') . '/templates';
  7. }


1 comment for 'Theming your views in your module on Drupal 7'

anonimo's picture

Great. Thanks for sharing :)

Great. Thanks for sharing :)

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <quote> <blockquote>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <pre>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.