Example: How to display Block view different from default Page view

Last modified: May 4, 2008 - 20:41

By default a view in drupal 5 displays in "block view" all the fields that it displays in the "page view"

as described by merlin
http://drupal.org/node/245234#comment-802873
one can use theming or create a different view

here is the solution using theming to get a different block view

function THEMENAME_views_view_table_VIEWNAME($view, $nodes, $type) {
 
  $fields = _views_get_fields();
  if ($type == "block"){
      foreach ($nodes as $node) {
        $row = array();
        foreach ($view->field as $field) {
          if ($field['label'] == "FIELDLABEL1" || $field['label'] == "FIELDLABEL2"){
              $cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
              $cell['class'] = "view-field view-field-$field[queryname]";
              $row[] = $cell;
          }
        }
        $rows[] = $row;
      }
//      return theme('table', array(''), $rows);  //use this if you want no table header
      return theme('table', $view->table_header, $rows);   

  }else if ($type == "page"){
      foreach ($nodes as $node) {
        $row = array();
        foreach ($view->field as $field) {
          $cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
          $cell['class'] = "view-field view-field-$field[queryname]";
          $row[] = $cell;
        }
        $rows[] = $row;
      }
      return theme('table', $view->table_header, $rows);   
  }
 
}

your view has to be formatted as table

change THEMENAME, VIEWNAME (optional - can get rid of it)
replace FIELDLABEL1 and FIELDLABEL2 with the labels that identify
the fields you want displayed

the "page" part of the code is the default behaviour (not changed)

The code is probably not the best available but I was looking for this for some time
and I thought that someone might find it useful.

 
 

Drupal is a registered trademark of Dries Buytaert.