Restored guide

Embedding a view in Drupal 6 using views_embed_view

Archived 2008–2011 · 2 min read · Eric London's Drupal Blog

views_embed_view() is the Drupal 6 Views 2 function that renders a display and returns HTML. This note is the small, safe version of that call.

Signature

The first argument is the view machine name. The second is the display id. Further arguments are contextual arguments, in the order the display expects them.

$html = views_embed_view('staff_list', 'block_1', $department_tid);

When you omit the display id, Views uses the default display. Be explicit. A later edit that adds a page display should not change a block you thought was stable.

Where to call it

Call it from a module or from preprocess, then print the string in a template. Do not call it from inside another View's header unless you have already ruled out a simpler attachment display. Nested views multiply queries.

function custom_block($op = 'list', $delta = 0) {
  if ($op === 'list') {
    return array('staff' => array('info' => t('Staff list')));
  }
  if ($op === 'view' && $delta === 'staff') {
    return array(
      'subject' => t('Staff'),
      'content' => views_embed_view('staff_list', 'block_1'),
    );
  }
}

Arguments and access

Arguments are not a permission system. A View that lists unpublished nodes will still list them if the display is not restricted. Set access on the display, and cast numeric arguments before you pass them.

Check before you deploy

  • Load the page with the Views query log enabled and confirm you are not embedding the same display twice.
  • Test as an anonymous user.
  • If the HTML is empty, the usual causes are a wrong display id, a required argument, or a filter that excludes every row.

Restored guide for the Drupal 6 views_embed_view() note cited from this blog.