Views Theme Function Tips

I'm assuming you've read about theming in Drupal, and theming views specifically.

What I often do is grab the code output by the views theme wizard, and then change the name to be more generic so that I can run a bunch of other views through it. Then I add some arguments to the function so that I can assign a class to the list. Notice the $attributes argument that then gets passed to the theme('item_list') function.

My generic function:

One of the major differences with this function from the standard one is that I have to grab the template file name from the view name, which is the first thing I do, and then pass that into _phptemplate_callback.

<?php function phptemplate_views_view_ul_list_with_class($view, $nodes, $type, $attributes = array()) { $template = 'views-list-'.$view->name; $fields = _views_get_fields(); $taken = array(); // Set up the fields in nicely named chunks. foreach ($view->field as $id => $field) { $field_name = $field['field']; if (isset($taken[$field_name])) { $field_name = $field['queryname']; } $taken[$field_name] = true; $field_names[$id] = $field_name; } // Set up some variables that won't change. $base_vars = array( 'view' => $view, 'view_type' => $type, ); foreach ($nodes as $i => $node) { $vars = $base_vars; $vars['node'] = $node; $vars['count'] = $i; $vars['stripe'] = $i % 2 ? 'even' : 'odd'; foreach ($view->field as $id => $field) { $name = $field_names[$id]; $vars[$name] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view); if (isset($field['label'])) { $vars[$name . '_label'] = $field['label']; } } $items[] = _phptemplate_callback($template, $vars); } if ($items) { return theme('item_list', $items, NULL, 'ul', $attributes); } } ?>

Then I place the correct function name in the template file and with that call my generic function, pass in the normal views arguments, and pass in the class and id for the ul:

*/ function phptemplate_views_view_list_titles_by_term_publications($view, $nodes, $type) { return phptemplate_views_view_ul_list_with_class($view, $nodes, $type, array('class' => 'brieflist', 'id' => 'by-term-publications')); } ?> field as $id => $field) { $name = $field_names[$id]; $vars[$name] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view); if (isset($field['label'])) { $vars[$name . '_label'] = $field['label']; } } $items[]= array( 'data' => _phptemplate_callback($template, $vars), 'class' => views_css_safe($node->type), ); ?>

You could extend this to passing in a class to the li as well, check out theme_item_list: http://api.drupal.org/api/function/theme_item_list/5 on how the lists are built.

Setting the class of the li could be set in the former function. You can run through the nodes to see what content type the node is, or whatever (get friendly with print_r($nodes) to see what data is available), and then add a variable to the item array like this:

field as $id => $field) { $name = $field_names[$id]; $vars[$name] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view); if (isset($field['label'])) { $vars[$name . '_label'] = $field['label']; } } $items[]= array( 'data' => _phptemplate_callback($template, $vars), 'class' => views_css_safe($node->type), ); ?>

Whether removing divs or adding classes its usually possible to do it through the theme functions.