Embedding a view

Last updated on
30 April 2025

You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules

/**
 * Embed a view using a PHP snippet.
 *
 * This function is meant to be called from PHP snippets, should one wish to
 * embed a view in a node or something. It's meant to provide the simplest
 * solution and doesn't really offer a lot of options, but breaking the function
 * apart is pretty easy, and this provides a worthwhile guide to doing so.
 *
 * @param $name
 *   The name of the view to embed.
 * @param $display_id
 *   The display id to embed. This will be 'embed_ID'.
 * @param ...
 *   Any additional parameters will be passed as arguments.
 */
function views_embed_view($name, $display_id = 'default') {

To figure out the id of a display, look under Other, in the
view edit page for the particular view that will be embedded, for Machine Name:
what ever this name is is what should be used for the $display_id variable.

Usage

Embedding a view

The best way to use embedded views is to create your own module and integrate
with which ever system you need to within Drupal, specifically
hook_preprocess_HOOK()

You can easily embed the results of a view into other parts of your site;
either, with code as a module, or in nodes or blocks as snippets. The
easiest way is to use the function views_embed_view():

print views_embed_view('test', 'embed_1');

This is the equivalent of views_embed_view without arguments.

$view = views_get_view('test');
$view->preview('embed_1');
$view->destroy();

Embedding a view with arguments

print views_embed_view('test', 'embed_1', arg1, arg2, ...);

This is the equivalent of views_embed_view with arguments.

$view = views_get_view('test');
$view->set_arguments(array(arg1, arg2, ...));
print $view->preview('embed_1');
$view->destroy();

Advanced examples

Embedding individual fields from a view

To retrieve a specific field from an embeded view is to use
$view->render_field($field, $row)

$view = views_get_view('test');
$view->set_display('embed_1');
$view->pre_execute();
$view->execute();

if (!empty($view->result)) {
  // Some fields may need to be pre_rendered before they are populated with
  // their values. Try without and if it does not work try with pre_render.
  //$view->field['field_example_field']->pre_render($view->result);
  foreach ($view->result as $row => $values) {
    $text = '';
    $text .= '<div class="name">' . $view->render_field('title', $row) . '</div>';
    $text .= '<div class="location">' . $view->render_field('field_example_field', $row) . '</div>';
  }
  return $text;
}
$view->destroy();

Using fields from nodes as arguments for views

$node = node_load($nid);

// Create an array of taxonomy terms from node.
$categories = array();
$categories = field_get_items('node', $node, 'field_categories');
$category_tids = array();
foreach ($categories as $category) {
  $category_tids[] = $category['tid'];
}

// Create arguments for view for "AND".
$arguments = implode(',', $category_tids);

$view = views_get_view('test');
$view->set_arguments(array($arguments));
print $view->preview('embed_1');
$view->destroy();

Help improve this page

Page status: Not set

You can: