Embed drupal views snippets

December 10th, 2010, 9:25H · Topics: drupal, snippets · Print

Embed a view:

<?php
$view
= views_get_view('viewname');
print
$view->execute_display('default', $args);
?>
Be sure the $args you pass is an array. I think you can just leave the argument off if you don’t have args at all.

And to explain furthur, you can change ‘default’ to whatever display id you want (‘page_1′, ‘block_1′, ‘block_2′), eg.:

<?php
$view
= views_get_view('viewname');
$display = $view->execute_display('block_1');
print
$display['content'];
?>

This is important if you want your embedded view to pick up any of your custom theming.

For reference, my snippet:

<?php
$url
= explode('/', trim(request_uri(), '/'));
$url[0]= str_replace( '?q=', '', $url[0]);

$args = array($url[1]);
print
views_embed_view('nodequeue_1', 'page', $url[1]);
?>

Just another way of doing it by creating a variable from template.php to embed a view in template files or through php filter. You can now use vars['embed_rooms_view_1'] anywhere as $embed_rooms_view_1. The js is rebuilt using drupal_get_js() to overcome an issue with if you are using views slideshow then to load the js in preprocess function.

<?php

function themename_preprocess_page(&$vars, $hook) {
$vars['embed_rooms_view_1'] = views_embed_view(‘rooms’, ‘block_1′);
$vars['embed_rooms_view_2'] = views_embed_view(‘rooms’, ‘block_2′);
$vars['scripts'] = drupal_get_js();
}
?>

Tags: , , , , ,

  1. 1 cross February 20th, 2011 19:28H

    Im usualy doing like this:
    $arg0 = arg(0);
    print view_embed_view(‘view_name’, ‘view_display_name’, arg0);

    And execute method is only in case I have to chage view settings instead of using default;

Leave a Reply

Your email address will not be published. Your photo in comments, use Gravatar
Please include http://
Note: XHTML is allowed.

Subscribe to this comment feed via RSS

Drupal Developer, Delhi

I provide drupal development service, consulting and training. Would love to work if your project offers me more then money. The project should give me goosebumps :)