Community Documentation

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

Last updated March 26, 2011. Created by GiorgosK on May 4, 2008.
Edited by linclark. Log in to edit this page.

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.

About this page

Drupal version
Drupal 5.x

Archive

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here