background image
Eric's picture

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);
?>

Thanks!...Eric

This article proved get time-saviour when i was in very urgent need for such helper function.Thanks for writing it so consice.

Thank you for posting this

It saved me a lot of work. (PS: This site is one of the best sources of good information on drupal configuration anywhere on the web.) Regards.

Changing Default settings before embedding view

How do i changed the default setting of "Items to Display" before embedding the view?

this is my current code:

<?php
print views_embed_view($my_view_name);
?>

show view on a view page???

Hey i found this article and i thing this is just what i need but don't really know how to implement this.

I have two views Products_overview and Product_news

the products_overview view is the default view used which displays a list of products. after a product has been selected all content types related to that product are being displayed using arguments. the product type Product news is also there but what i would want is to display by Product news View under the list of content types related to the product so the product news related to the selected product will be displayed. I think this can be done with the view embed function.

any help would be awesome

question about views_embed_view

hi, im new to drupal, and your site is a great resource for me! thanks :)

i have noted(if im correct) that when views_embed_view returns all the content from the view, my quest is how can i manage the different variables to display it as i want(and not all the content at once), or i have to call another function?

Eric's picture

hooks

Views has some great hooks for modifying views objects: http://views2.logrus.com/doc/html/index.html

This is just great!!

Thanx a lot! Is it possible to pass arguments as well? I have 1 main view - 1 page, filtering using arguments for taxonomy term location (usa, japan, brazil etc..), and want to embed another view using the same arguments.. so both of the views will be filtered using the same argument in URL, thanx a lot again, saved a lot of time..

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
        )

)

Eric's picture

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);
?>

views_get_view()

By the way, you have empty array $view->result too

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?

Eric's picture

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

Theming embedded view

Are you sure, theming an embedded view works like that? If I open my view page theming with the template file works but when I create a node with a views_embed_view php snippet the theming is not applied. Any idea?

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?

Eric's picture

hmm

Can you send a screen shot?

Thanks! This was exactly

Thanks! This was exactly what I was needing.

Eric's picture

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;   
}
?>

So if I want to access view information...

<?php
$view
= views_get_view('home_news');

 
// ensure view exists
 
if (!$view) print("Not real");

 
// set display_id
 
$view->set_display('block');

 
// execute view
 
$view->execute();
 
$viewArray = $view->result;
  echo(
"<pre>"); print_r ($viewArray); echo("</pre>");

     foreach(
$viewArray as $item){
       
$node = node_load($item->nid);
        print
'<h3 id="'.$node->nid.'">'.$node->title.'</h3>';
        print
$node->body;
}
?>

But where does that leave custom content types?

$node->data->subtitle?

I dunno

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

Eric's picture

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

And this ?

<?php
$view
= views_get_view("visual_articles");
$view->set_items_per_page(24);
$view->set_use_pager(true);
$view->execute();
$node->articles_view = $view->render();
?>

:)

Eric's picture

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

Eric's picture

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

Eric's picture

thanks

nice catch, thanks!

Where can i place the above

Where can i place the above sniped to actually work

Eric's picture

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.