Today I tried to embed the HTML from a View in a page layout following this guide. Apparently, in Drupal 6 the views_build_view function has been replaced by views_embed_view. Here's a code snippet that lets you to embed a view:
<?php
$viewName = 'MYVIEWNAME';
print views_embed_view($viewName);
?>The views_embed_view has 2 default arguments. The second argument allows you to enter the display_id of the view (example: default, page, block, etc). Any additional argument you specify will be passed to the views argument handler. For instance, if you wanted to embed the block view and pass it a list of nodeIDs as an argument...
<?php
$viewName = 'MYVIEWNAME';
$display_id = 'block';
$myNodes = array(1, 2, 3);
print views_embed_view($viewName, $display_id, $myNodes);
?>












The $view object
Hello Eric! Do you know how to get data from Views 2 ?
I use $view = views_get_view('view_name');
Then print_r($view);
+ I wrote it in page.tpl.php
But there is no data here! Only something like that http://www.flickr.com/photos/kentbye/511817875/sizes/o/
I want to get something like that http://drupal.org/handbook/modules/views/api#comment-247361
Can I get this (array below) using the "views_get_view" function ?
Array
(
[0] => Array
(
[field1] => foo
[field2] => foo
)
[1] => Array
(
[field1] => foo
[field2] => foo
)
)
views_get_view()
You might want to try using views_get_view()...
<?php
$view = views_get_view($viewName);
echo "<pre>" . print_r($view->result, true) . "</pre>";
// krumo($view);
?>
How to print only 1 element of the output
I got this post after so much digging. I know this is what I need to do.
I have set up a content type to add client logos. In a particular node's particular area, I want to print the latest 3 logos. I am able to create a view as well as print the output of "views_embed_view" like you have mentioned. But, now I want to apply CSS styles to each of the result elements. I tried the 'Title' method that you have explained. But for me the images are still getting out put together. Is there a way I can use a foreach or something? How can I get to know each of the output array elements?
view theming
If you are trying to show the latest 3 logos, I would edit the view settings and change the default sort order to "Node published" (sorted descending), and limit the number of results to 3.
In regards to theming the output of your view, go to your view edit screen (admin/build/views/edit/VIEWNAME), and check out the section called "Theme: Information" under Basic Settings. If you'd like a custom layout for the output, you could change the view style to "unformatted", create a theme template file called "views-view-fields--VIEWNAME.tpl.php", and alter the html and css in this file.
hope this helps!
Eric
Need help embedding a view and preserving the table formatting
<?php$viewName = 'MYVIEWNAME';
$display_id = 'block';
$myNodes = array(1, 2, 3);
print views_embed_view($viewName, $display_id, $myNodes);
?>
This snippet works, but it doesn't preserve the table formatting of the block view. How can I accomplish that?
hmm
Can you send a screen shot?
Thanks! This was exactly
Thanks! This was exactly what I was needing.
More Information
views_embed_view does not show the view's title. Here's how you can get the title from the view object:
<?php
$viewName = 'MYVIEW';
$viewHtml = views_embed_view($viewName);
if ($viewHtml)
{
// load view object
$view = views_get_view($viewName);
$title = $view->display['default']->display_options['title'];
$page_contents .= "<h2>$title</h2>";
$page_contents .= $viewHtml;
}
?>
How to hide the title when the result is empty?
Hello,
I tried your solution and it’s working great.
My view can display an empty result, so only the title is showing. I didn’t find how hide my title when the result of my view is empty
My view is always returning me a result even if there nothing to display
Could you help me?
Best regards
jQuery hover html
My code was originally written for Drupal 5, when the hover links were not included in the result. You can either turn off the jQuery admin links, or update the conditional statement. Here is a more accurate way of testing if the view has results:
<?php
// create string variable for page content
$pageContents = "";
// set the view name
$viewName = 'test_view';
// fetch the view object
$view = views_get_view($viewName);
// get the title for the view, if there are results
if (is_object($view) && count($view->result)) {
$title = $view->display['default']->display_options['title'];
if ($title) $pageContents .= "<h2>$title</h2>";
}
// add view html to page contents
$pageContents .= views_embed_view($viewName);
return $pageContents;
?>
Thanks for this tip!
I'm fairly new to Drupal, and I keep on finding cool things that make my life so much easier!
Sadly (as in this case), I usually find these tips after spending many hours struggling to do the same things with my own code.
seting the items per page to a view
How can we set the items per page to a view, programatically?
The simila code we are using in drupal 5 is as follows:
$view = views_get_view('visual_articles');
print (views_build_view('embed', $view, array(0 => '1', 6 => $key[1), $view->use_pager, $view->nodes_per_block));
Can u please give me the corresponding code in druapal 6
Thanks in advance.
Cibi
cibi.jacob@gmail.com
pager
Hi Cibi,
I have not tried to do this yet with Drupal 6. You might want to try loading the views object and then setting this variable: $view->pager['items_per_page']
Hope that helps!
-Eric
correction...
$view->display_handler->set_option('items_per_page', 20);
print $view->preview();
You'll need to do
You'll need to do a
$view->set_display($display_id);
before calling
$view->display_handler->set_option('items_per_page', 20);
otherwise you get a Fatal error: Call to a member function set_option() on a non-object
omg i think I love
omg i think I love you...
I've been looking for this on and off for weeks! weeks I tell you, and now, it works...
so beautiful I just might cry
thanks
nice catch, thanks!
Where can i place the above
Where can i place the above sniped to actually work
options
The easiest way to embed this is to create a node and choose PHP for the input filter. I prefer to create a module that contains a hook_menu() and page callback.