Submitted by Eric Weik on Mon, 01/04/2010 - 22:40.
Although I do not specifically discuss this feature in my original post, views_embed_view() accepts additional parameters, one of which changed slightly between 6.x-2.7 and older versions.
Lets first look at the function definition and documentation for views_embed_view():
<?php /** * 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. * * Note that this function does NOT display the title of the view. If you want * to do that, you will need to do what this function does manually, by * loading the view, getting the preview and then getting $view->get_title(). * * @param $name * The name of the view to embed. * @param $display_id * The display id to embed. If unsure, use 'default', as it will always be * valid. But things like 'page' or 'block' should work here. * @param ... * Any additional parameters will be passed as arguments. */ function views_embed_view($name, $display_id = 'default') {} ?>
In my original example, we were only using the $name parameter, which pulled the default display of a view. However, if you have a view with multiple displays (e.g. "Default" for the default view, "Some Page View" for a page view, and "Some Block View" for a block), you can load the specific display by passing in $name and $display_id with a calls like:
<?php // Fetch the default view for output: $defaultfeed = views_embed_view('recent_blog_posts');
// Fetch "Some Page View" for output: $pagefeed = views_embed_view('recent_blog_posts','page_1');
// Fetch "Some Block View" for output: $blockfeed = views_embed_view('recent_blog_posts','block_1'); ?>
In versions of views prior to 6.x-2.7, you could actually use the human readable name of the display instead of the identifier:
<?php // *** Don't do this, it does not work in Views 6.x-2.7 or later!! *** $rawfeed = views_embed_view('recent_blog_posts','Some Page View'); ?>
However, this is not the correct usage, and after some changes to the display handler, this usage will result in an empty view result.
If you run into this problem, you should simply be able to update to use the proper display identifier ('page_1', 'page_2', 'block_1', block_2', etc.).
Hello! My name is Eric Weik. I am a computer scientist, photographer, musician, and occasional blogger. New Rivers Digital is my software consulting business. I am dedicated to using open source software and open data standards for Web development and applications integration. In particular, I am an ardent Drupal fan and specialize in Drupal module development, theming, and data architecture integration.
Reply to comment
#1 Clarification of views_embed_view() parameters in 6.x-2.7
Submitted by Eric Weik on Mon, 01/04/2010 - 22:40.
Although I do not specifically discuss this feature in my original post, views_embed_view() accepts additional parameters, one of which changed slightly between 6.x-2.7 and older versions.
Lets first look at the function definition and documentation for views_embed_view():
<?php/**
* 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.
*
* Note that this function does NOT display the title of the view. If you want
* to do that, you will need to do what this function does manually, by
* loading the view, getting the preview and then getting $view->get_title().
*
* @param $name
* The name of the view to embed.
* @param $display_id
* The display id to embed. If unsure, use 'default', as it will always be
* valid. But things like 'page' or 'block' should work here.
* @param ...
* Any additional parameters will be passed as arguments.
*/
function views_embed_view($name, $display_id = 'default') {}
?>
In my original example, we were only using the $name parameter, which pulled the default display of a view. However, if you have a view with multiple displays (e.g. "Default" for the default view, "Some Page View" for a page view, and "Some Block View" for a block), you can load the specific display by passing in $name and $display_id with a calls like:
<?php// Fetch the default view for output:
$defaultfeed = views_embed_view('recent_blog_posts'); // Fetch "Some Page View" for output:
$pagefeed = views_embed_view('recent_blog_posts','page_1'); // Fetch "Some Block View" for output:
$blockfeed = views_embed_view('recent_blog_posts','block_1');
?>
In versions of views prior to 6.x-2.7, you could actually use the human readable name of the display instead of the identifier:
<?php// *** Don't do this, it does not work in Views 6.x-2.7 or later!! ***
$rawfeed = views_embed_view('recent_blog_posts','Some Page View');
?>
However, this is not the correct usage, and after some changes to the display handler, this usage will result in an empty view result.
If you run into this problem, you should simply be able to update to use the proper display identifier ('page_1', 'page_2', 'block_1', block_2', etc.).
For more information, see the following issue: http://drupal.org/node/624144.
Reply
About
Hello! My name is Eric Weik. I am a computer scientist, photographer, musician, and occasional blogger. New Rivers Digital is my software consulting business. I am dedicated to using open source software and open data standards for Web development and applications integration. In particular, I am an ardent Drupal fan and specialize in Drupal module development, theming, and data architecture integration.
Contact Details
Lancaster, VA 22503
Content Tags
Abstract B&W Celestial Clouds Drupal 6 Drupal Recipes Drupal Sites Estuary Etsy Government Grasses HDR Long Exposure Macro New Rivers Digital Orton Photoblog Photo Expedition Photoset Renderblog RGB Sketchbook Snow Storm Structure Synth Sunflow Sunset Theming Webform.module Wide Angle Zen Zen ThemingRecent comments
Popular content
Today's:
All time:
Activity Stream
Sun, 01/29/2012 - 22:31
Mon, 01/16/2012 - 10:43
Fri, 01/06/2012 - 07:54
Powered by Drupal & Genesis | Valid XHTML 1.0 Strict |
RSS Feed
© 2010 New Rivers Digital | PO Box 784 | Lancaster, Virginia 22503 | +1-804-577-8526 | Contact Form