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 arguments...
<?php
$viewName = 'MYVIEWNAME';
$display_id = 'block';
$myArgs = array(1, 2, 3);
print views_embed_view($viewName, $display_id, $myArgs);
?>
Or, to implode numerous values into a single argument:
<?php
$viewName = 'MYVIEWNAME';
$display_id = 'block';
$myNodes = array(1, 2, 3);
print views_embed_view($viewName, $display_id, implode('+', $myNodes));
?>
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
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
print $view->preview();
For example I have a view which lists all blog posts on the site and I want it to be in the block when I enable the module (the view will return the empty message if none are there of course)
I would most appreciate your advise - I see you have done some great things with views here.
Thanks
Works fine for me when i delete argument from views and it displays all arguments but i do need to have list of related by term materials, its my catalogue based on taxonomy. So need to have a block/field listing all other articles with the same tag
$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
Any idea on how to load ajax so the pager doesn't do a page refresh everytime?
Thanks much!
Sradox
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
You can get around this by setting $view->is_attachment = TRUE; before calling preview().
See: http://drupal.org/node/734096#comment-2703886
:)
Sadly (as in this case), I usually find these tips after spending many hours struggling to do the same things with my own code.
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
This will show the view, with title, only if there are results.
Template.php
Then in your tpl.php:
- The guy next to you.
This will show the view, with title, only if there are results.
Template.php
Then in your tpl.php:
- The guy next to you.
But where does that leave custom content types?
$node->data->subtitle?
I dunno
This snippet works, but it doesn't preserve the table formatting of the block view. How can I accomplish that?
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?
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
Just wondering what your thoughts are on this approach. Is it bad form to put this in a template file? Is rendering a block view here a bad idea since it's not actually in a block? And... is my code lousy in general? As you know, I haven't really be coding very long : )
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 ?
but my output is
So $view->result is empty array!
Maybe function views_get_view() do not contain information like that ...
Maybe it is only about view's settings but not gained data from nodes
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?
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
this is my current code:
not the first time i am visiting your homepage as i was redirected by that search engine. your article hit it in the black.
tkx man
Wils
Thanks for your helpful blog!
How can I embed a view with a filter applied?
I tried this code:
But I get this output:
Any clue what I am missing?
print_r($view->result);
try:
print($view->preview);
This should return the parsed HTML, which I think is what you want?
1. default
2. page
What I need is to pass the output of default as default argument of page. How would be possible by php and hence need some direction or hints. Uptill now I have been to come up with the following code which is partially solving my problem:
This gives my the required argument string from the default but the page does not run may be due to the execute command. Please confirm what would be workaround so that I can execute the main view and not the view that is being use to generate the default arguments.
I can create the views with the exact same name as the node machine name is my reasoning.