Example: How to Embed Two Views on the Same Page

Last modified: June 24, 2009 - 14:03

In this view, the view takes an argument of the Node Title type, set to 1 letter long. This means that views/example/A will give all matching nodes that start with the letter A. What I really want,
though, is for the top of the view to contain the summary of the view and for the bottom of the view to contain the current letter.

The easiest solution is to put the summary view in the header of the view, but one MUST be careful here; embedding a view within a view could lead to an infinite loop as it tries to replicate itself to infinity. Fortunately for us, the summary is themed separately from the main view, and doesn't include header.

So what we'll do is put put a PHP snippet into the header of the view and set the Input Filter to PHP code.

This view needs to have just one argument; it's possible to do this with more arguments but you need to theme the summary as well, and check where you actually are. Be sure that argument is set to provide a summary!

<?php
 
// load a second copy of the view -- unfortunate but we don't have access
  // to the existing one.
 
$view = views_get_view('VIEWNAME');

 
// the array() indicates no arguments, which will provide the summary view.
  // We also don't want the pager for this view.
 
print views_build_view('embed', $view, array(), false, false);
?>

See also the example of the comma separated list summary, which goes well with this.

Variation: How to Embed Two Different Views with Same Argument

Let's take two different views, view 1 and view 2, that have exactly the same arguments. You want the top of view 1 to contain not the summary of view 2, but view 2 itself, with the same arguments view 1 is currently being provided.

The solution is to put the following PHP snippet into the header of view 1 and set the corresponding Input Filter to PHP code:

<?php
// This is a new variable introduced in Views module revision v.1.94;
// it conveniently gives one access to the current view (in this example, view 1)
global $current_view;
// Let's now load view 2
$view = views_get_view('VIEW2NAME');
// $current_view->args contains the arguments view 1 is currently being provided with;
// we can now build view 2 with exactly the same arguments as view 1
print views_build_view('embed', $view, $current_view->args, false, false);
?>

Note: if you have multiple Views applied to your home page, and the secondary views (the ones being called into the main View) don't have URLs entered, it will cause the 404 page page to show a secondary View instead of the 404 page content. To solve this, simply enter URLs for all the Views being applied to the page.

Site Recipe - HOWTO: Create mulitple Views on one node with paging - check it out here: http://drupal.org/node/85720.

doesn't work in 6.x - yet

eelkeboezeman - November 26, 2009 - 18:41

views_build_view throws: Call to undefined function views_build_view()

see this issue: http://drupal.org/node/234621

 
 

Drupal is a registered trademark of Dries Buytaert.