Example: How to Make Block View Behave Differently from the Same Page View

Last modified: May 1, 2006 - 15:28

Note: Views now lets you specify a list view for blocks independant of the page so this example illustrates a good point but not actually needed to achieve the end.

Let's say you have a simple view that is something like 'newest posts in with some taxonomy TERM.'. You want the view as both a page view and a block view, and you want the [more] link. No problem you say. But then you discover that they both must be the same type -- but you really want
it to be a list view as a block, and a teaser view as a page. No problem.

First, set the view up as a List View by default. Tell it to include the [more] link. We'll override its default behavior in page view.

Ordinarily, the [more] link only appears if it thinks there are actually more entries to see; we don't want that behavior in this case because the [more] link is going to different information.

<?php
function phptemplate_views_view_VIEWNAME($view, $type, $nodes) {

  if (
$type == 'page')
   
// Done before theming so theme can change it if it wants.
   
drupal_set_title(views_get_title($view));

  if (
$view->header) {
   
$header = check_markup($view->header, $view->header_format, false);
   
$output = "<div class='view-header' id='view-header-$view->name'>$header</div>\n";
  }

  switch (
$type) {
    case
'block': // List View
     
if (count($nodes) > 0) {
       
$output .= theme('views_view_list', $view, $nodes);
        if (
$view->block_more && count($nodes) >= $view->nodes_per_block) {
         
$output .= theme('views_more', $view->url);
        }
      }
      else {
        return;
      }
      break;
    case
'page':
     
$output .= theme('views_view_table', $view, $nodes);
      break;
  }

  return
"<div class='view' id='view-$view->name'>$output</div>\n";
?>

 
 

Drupal is a registered trademark of Dries Buytaert.