Theming Views in Drupal 6

Any reasonably complex Drupal website involves using Views for listing items and CCK for the creation of custom content types. Since each website has a unique look, a large part of creating a Drupal website is theming Views and CCK nodes. This is especially true for Views, since most CCK theming can be done via CSS (or for more complex stuff it goes to Forms API, beyond theming), but Views usually needs to be themed. Drupal theming has changed with Drupal 6 and so has the Views theming with Views 2 that ships with Drupal 6.

Irakli Nadareishvili
#Drupal | Posted

Any reasonably complex Drupal website involves using Views for listing items and CCK for the creation of custom content types. Since each website has a unique look, a large part of creating a Drupal website is theming Views and CCK nodes. This is especially true for Views, since most CCK theming can be done via CSS (or for more complex stuff it goes to Forms API, beyond theming), but Views usually needs to be themed. Drupal theming has changed with Drupal 6 and so has the Views theming with Views 2 that ships with Drupal 6. Below is a quick introduction to the new theming approach in Drupal 6 and Views 2. We assume that readers are intimately familiar with Views theming in Drupal 5, hence we will only highlight the differences. Most views theme functions, like the familiar theme_views_view_VIEWNAME($view, $type, $nodes, $level = NULL, $args = NULL) are now gone! New theming functions are as follows: Recordset theming:  theme_views_view_unformatted__VIEWNAME($view, $options, $rows, $title); theme_views_view_list__VIEWNAME($view, $options, $rows, $title); theme_views_view_grid__VIEWNAME($view, $options, $rows, $title); theme_views_view_table__VIEWNAME($view, $options, $rows, $title); theme_views_view_summary__VIEWNAME($view, $options, $rows, $title); theme_views_view_summary_unformatted__VIEWNAME($view, $options, $rows, $title); theme_views_view_rss__VIEWNAME($view, $options, $rows, $title); Individual item theming:  theme_views_view_row_comment__VIEWNAME($view, $options, $row); theme_views_view_row_node__VIEWNAME($view, $options, $row); theme_views_view_row_search__VIEWNAME($view, $options, $row); theme_views_view_row_fields__VIEWNAME($view, $options, $row); And special theming functions for:  theme_views_exposed_form__VIEWNAME ( $form ); theme_views_more__VIEWNAME($more_url); As you can guess, the starting “theme” in the function name should be substituted by theme name, if you are implementing it in a theme, or with “phptemplate” if you are implementing in a module CAUTION: Views2 is heavily cached, so you need to clear cache after any change (like adding a new theme function or modifying theme type + function) to see the updated results. Installing Devel module helps.

UPDATE: tpl.php theming By popular demand (heh), we are adding couple words about theming Views2 from tpl.php files: To get examples of tpl.php implementations you don’t need a documentation it’s right there in front of you (ok, maybe a little bit hidden) When you are in a view edit page, click on “Theme: Information” under Basic settings, it will show you all possible template files that the view could use (depending on – what level do you wish to override on). The tpl.phps that are actually being used will be in bold. For each set the title of set is hyprlinked (e.g. “Row style output”). Click on that and you will see a sample tpl.php of that group/set. Those are extremely helpful, pretty self-explanatory, too. You can also find these tpl.php’s in the views module, under the “theme” folder. I think you will like what you find there.

* * *

Irakli Nadareishvili