Customising table views generated by the views.module

description

The following snippets illustrate how you can override the default table views output from the views.module using snippets you can copy and paste into your template.php file.

Please also note the following handbook page Theming table views using your style sheet (.CSS)

Method 1 of 2 - Theme ALL table views using your TEMPLATE.PHP file

The snippet below, illustrates how to override the default theme for *all* table views in your site. Paste this into the TEMPLATE.PHP file in your theme folder and change to suit.

This example adds the attributes cellspacing='4' border='1' to all table views on your site.

<?php
/**
* This snippet will override all table views in your site
* Paste this into your template.php file and change the viewname in the first line
* and the attributes in the second last line of the snippet.
*/

function phptemplate_views_view_table ($view, $nodes, $type) {
 
$fields = _views_get_fields();

  foreach (
$nodes as $node) {
   
$row = array();
    foreach (
$view->field as $field) {
      if (
$fields[$field['id']]['visible'] !== FALSE) {
       
$cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
       
$cell['class'] = "view-field ". views_css_safe('view-field-'. $field['queryname']);
       
$row[] = $cell;
      }
    }
   
$rows[] = $row;
  }
  return
theme('table', $view->table_header, $rows, array('cellspacing'=>'4', 'border'=>'1'));
}
?>

Method 2 of 2 -Theme a specific table view using your TEMPLATE.PHP file

The snippet below, illustrates how to theme a speicific table view in your site. Paste this into the TEMPLATE.PHP file in your theme folder and change to suit.

This example adds the attributes cellspacing='4' border='1' to the viewname table view on your site.

Where viewname is the unique identifier of the view or the name you gave the view in the Basic Information options. Click on ADMINISTER -> VIEWS to see a list of your Existing Views. The unique identifier or view name is in the first column.

<?php
/**
* This snippet wil override a specific table view in your site
* Paste this into your template.php file and change the viewname in the first line
* and the attributes in the second last line of the snippet.
*/

function phptemplate_views_view_table_viewname($view, $nodes, $type) {
 
$fields = _views_get_fields();

  foreach (
$nodes as $node) {
   
$row = array();
    foreach (
$view->field as $field) {
      if (
$fields[$field['id']]['visible'] !== FALSE) {
       
$cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
       
$cell['class'] = "view-field ". views_css_safe('view-field-'. $field['queryname']);
       
$row[] = $cell;
      }
    }
   
$rows[] = $row;
  }
  return
theme('table', $view->table_header, $rows, array('cellspacing'=>'4', 'border'=>'1'));
}
?>

Notes

  • See also the following handbook page Theming table views using your style sheet (.CSS)
  • The above snippets are only for use with the views.module.
  • More snippets will follow, including how to override the default cell/row theme layout of table views.

Is it possible to spread the

appel - August 7, 2007 - 12:19

Is it possible to spread the data of one single row over 2 rows?

Like so:


--------------------
| date 1 | title 1 |
--------------------
| author 1 | link 1 |
--------------------
| date 2 | title 2 |
--------------------
| author 2 | link 2 |
--------------------

'What's a Texas?'

Spread data on multiple rows

pingers - September 13, 2007 - 05:37

Yes, you need to change the way the $row is constructed... i.e. add the pieces you want to each $row. (using $row[] = )

This is the part you need to play with:

  foreach ($nodes as $node) {
    $row = array();
    foreach ($view->field as $field) {
      if ($fields[$field['id']]['visible'] !== FALSE) {
        $cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
        $cell['class'] = "view-field ". views_css_safe('view-field-'. $field['queryname']);
        $row[] = $cell;
      }
    }

Here's an override I've used

Rowanw - November 26, 2007 - 01:15

The table uses 5 fields including a CCK field called 'field_hotel_rating_value', which returns a simple number from 1-5. I wrapped an image around the value so the source of the image is affected by the field's value.

<?php
function phptemplate_views_view_table_hotel_listing($view, $nodes, $type) {
 
$fields = _views_get_fields();

  foreach (
$nodes as $node) {
   
$row = array();
    foreach (
$view->field as $field) {
     
//print $field['field'] .' | '; // field names
     
if ($fields[$field['id']]['visible'] !== FALSE) {
        switch (
$field['field']) {
          case
'field_main_image_fid';
           
$cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
           
$cell['class'] = "view-field ". views_css_safe('view-field-'. $field['queryname']);
           
$row[] = $cell;
          break;
          case
'title';
           
$cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
           
$cell['class'] = "view-field ". views_css_safe('view-field-'. $field['queryname']);
           
$row[] = $cell;
          break;
          case
'body';
           
$cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
           
$cell['class'] = "view-field ". views_css_safe('view-field-'. $field['queryname']);
           
$row[] = $cell;
          break;
          case
'field_hotel_rating_value';
           
$cell['data'] = '<img src="/files/images/star-'. views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view) .'.gif" alt="star rating" />';
           
$cell['class'] = "view-field ". views_css_safe('view-field-'. $field['queryname']);
           
$row[] = $cell;
          break;
          case
'view';
           
$cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
           
$cell['class'] = "view-field ". views_css_safe('view-field-'. $field['queryname']);
           
$row[] = $cell;
          break;
        }
      }
    }
   
$rows[] = $row;
  }
  return
theme('table', $view->table_header, $rows);
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.