Embed View :: How the heck do we embed views in nodes in D6?
pegleglax - April 15, 2008 - 03:15
| Project: | Views |
| Version: | 6.x-2.0-rc3 |
| Component: | Miscellaneous |
| Category: | support request |
| Priority: | normal |
| Assigned: | pegleglax |
| Status: | closed |
Jump to:
Description
Hello everyone,
I'm can't wait for Panels, Views, and CCK to be finished in D6. Until then, I need a way to manually embed views within nodes using the PHP filter option on nodes in Drupal 6. Anyone know how to do that?
Please do not refer me to these pages:
http://drupal.org/node/47417
http://drupal.org/node/48816
I get undefined function calls: Fatal error: Call to undefined function views_build_view()
THANKS! LOVE THE COMMUNITY!

#1
Try this:
<?php$view = views_get_view('viewname');
print $view->execute_display('default', $args);
?>
#2
#3
merlin I absolutely love you! thanks! and GREAT work on views!!! The UI has made leaps and bounds!
#4
merlin, check this out: (its my testing site)
pegleglax.homeip.net/sertoma
see the error views creates?
#5
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.
#6
works!!!! Thanks!
<?php$view = views_get_view('viewname');
print $view->execute_display('default');
?>
#7
Automatically closed -- issue fixed for two weeks with no activity.
#8
what should i do if I want to embed 2 views on same page?
#9
I figured out, Thanks
#10
And just to elaborate, 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 necessary if you want your embedded view to pick up any of your customised theming.
#11
Has anyone else had issues with node titles being changed when you call execute_display? I'm calling it at the end of my node.tpl.php, and it seems to somehow be overriding the node's title with it's own, even though the title field should have all ready been printed. It's like the call to execute_display is getting the entire theme engine reset and redone. I'll keep on it, but I was hoping somehow that this isn't an issue with views and is just my site :)
Thanks,
--Andrew
#12
You should create a new issue for this. It's a different question to the one in the parent post, which has been resolved. Be nice to merlin.
#13
Never ever ever use execute_display() to embed a view. Always use preview(). See views_embed_view() for the canonical example.
view::execute_display() should only be used where the display handler was intended to be used. i.e, a 'page' display should only use execute_display via a proper page callback, and a 'block' display should only use execute_display via hook_block.
#14
Thank you - calling views_embed_view() is working perfectly!
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]);
?>
--Andrew
#15
I think you'll find that the first part of all that can be eliminated by using the arg() function.
arg(1) perhaps.
#16
Yeah, I used to hope so too Merlin :). The problem is that I'm grabbing the argument from a URL alias set with the pathauto. When you call arg(), you get the internal system path of the node you're looking at, and last I looked there was no Drupal API to easily get the unchanged path to the current page. Though it's probably better to call drupal_get_path_alias() with $_GET['q'], and then you avoid the removal of ?q=.
--Andrew
#17
actually no, you're right the first time. If you call drupal_get_path_alias() you'll do a database lookup and if it's a URL with multiple aliases you will always get the same one regardless of which alias was actually used.
#18
I'd like to hook a filter into this view in my node-games.tpl.php file as I can't figure out how to do this with argument handling directly in the views 2 admin tool. Can I do something like this? Can't seem to get it to work. So close.....Yet so far.
<?php$sql = "SELECT field_hometeam_nid FROM {content_type_games} WHERE nid = %d";
$myhometeam = db_result(db_query($sql, arg(1)));
$view = views_get_view('schedules_home');
function yourmodule_views_query_alter(&$view, &$query) {$query->where[0]['clauses'][3] = "node_data_field_hometeam.field_hometeam_nid = '$myhometeam'";}
print $view->execute_display('Block');
?>
If not, where can I place this function so that this works? FYI - Basically what this is saying is "Take a [game content type] node and find all the games for the home team [home team is a node reference field to a [team content type] node that references the home team for the game. Then I am trying to render this view in a block within the game node [Displays the home team's season schedule]
Thanks for any help.
Tim
#19
Sorry if this is closed. But it's the nearest issue I have. I just don't want to hijack nor duplicate, only extend the question.
I managed to display views of the current user using this:
views_embed_view('status', 'block_1', $uid)
But I want to display the limit in that line as well, say to 1 or 3 nodes, while at the same I can have the same block with unlimited nodes display.
After reading the function views_embed_view in the module, and searching all around the world, I give up now, really.
I tried views_embed_view('status', 'block_1', $uid, 1) and views_embed_view('status', 'block_1', $uid, $limit = 1) and several blind break downs to no luck.
Please tell me if this is possible with that function to add the limit. If not how do I make it?
In the meantime, I have two options:
1. Two different blocks with each own Items to display.
2. Two different views with different Items to display.
But I considered both tedious. Any easier way?
Thanks.
#20
You should just be able to do a theme override for your view. I haven't pulled up code, but I'm sure that you can do something like sizeof($view->rows) or whatever contains all of the nodes to theme. It shouldn't have anything to with embedding specifically.
--Andrew
#21
What's tedious in that? Just add another "Block" display to your view, and override its "item per page" option. Nothing tedious. Then pass 'block_2', or whatever, to views_embed_view().
You could try:
$view = views_get_view('status') or die('No "status" view.');$view->set_display('block_1');
$view->display_handler->set_option('items_per_page', 3);
print $view->preview('block_1', array($uid));
I haven't tried this, though. Expect typos. The "tedious" way would be the better choice for non-programmers.
#22
You are right. Thanks for the light.
#23
I embed my view with this code:
<?php$view = views_get_view('viewname');
$display = $view->execute_display('block_1');
print $display['content'];
?>
But how can I style this view?
I created view and I putted it on my page but it looks awful - how can I style it?
#24
...using a view-[viewname}.]tpl.php template in your theme folder...
You could also wrap a tag around your php code and give it a specific ID. Then you could apply some CSS styling,....
#25
Hi,
I have problems with some divs that I dont need; some divs like:
<div class="view-content"><div class="views-row-1 views-row-odd views-row-first views-row-last">
and I dont need those.
I checked all my tpl.php files of my view and there is no divs like this - it can be that this divs are served with this 'default'
in this line
<?php
$view = views_get_view('Stories_on_frontpage');
print $view->execute_display('default');
?>
Edit: Added
<code>tags so the divs didn't get stripped by the input filter.Where I can style this default display of a view?
#26
Ok... here is the solution.
1) Go to your views modules directory (mine is in sites/all/modules/views/).
2) Open the "theme" directory (again, mine is sites/all/modules/views/theme/)
3) Copy any tpl.php's you need to you theme directory... for example: sites/all/themes/[your_theme_directory]
The main view template is views-view.tpl.php... and the others are special cases or sub-components...
4) Modify them till your heart is content. BUT, DO NOT modify the default tpl.php's in the views directory.... make sure you copy theme and THEN modify the copies.
::::::::::::::NOTE::::::::::::
Modifying, for example, the views-view.tpl.php will modify all views across your entire site by default. If you specifically wanted to modify a view called... say... stories-on-frontpage.... then you could create a template like so: views-view-stories-on-frontpage.tpl.php. Now, I'm GUESSING on the syntax... meaning I know that that IS the way to do it... but I don't know if the way I spelled/wrote views-view-stories-on....tpl.php is correct. Some research in the documentation should tell you... That should take about 15 minutes.
Need any more help?? Just ask! I'll answer what I can :)). But for now, I'm going to mark this as fixed.
#27
I just looked at your "tracker" and saw your other posts... you want to have 2 columns on your home page?? Ok... here's how most drupalers would do it. I'm going to lay it out briefly and basically because I don't have a lot of time and you will learn more figuring it our yourself.. But here are some basic guidelines.
Modules:
---------------------------
Panels
Views
Note: Both of these are written by the aMaZinG merlinofchaos ;)
1) Create a Panel Node.
2) After you create the basic node (at this point it doesnt have to show any content)... Go to { Administer > Site Configuration > Site Information } and change "Default front page:" to point to that node.
3) Now, your home page will show the panel. I'm guessing you will have chosen a two column layout. So each of those columns need sorted lists of other nodes... right? Here's where views comes in.
4) Each column will have its own view. So make nodes that go in each, then add those nodes to the views you create. Then, add the views to the panel under the "Panel Content" tab on the panel page (in this case the front page). To get them to be available, check the box under { Administer > Panels > Views Panes}....
And walaaaa!
Now if you want to get even more crazy..... instead of assigning the Panel to be the home page, you can use the Front Page module http://drupal.org/project/front. You can embed the panel inside the front page that way....
Sorry.. I don't have any more time at the moment. I Hope this helped!!!
#28
pegleglax - I did that. All that.
But I still have some divs that are displaying on my page and I dont see them in those .tpl.php files.
I checked all my tpl files and I dont see those divs.
Can you tell me what mean that 'default' in view displaying?
#29
please copy/paste the HTML here so I can take a look at it... and comment on which divs are frustrating you
#30
div class="view-content"
div class="views-row-1 views-row-odd views-row-first views-row-last"
Those divs are frustrating me.
#31
sites/all/modules/views/theme/views-view.tpl.php
<div class="view view-<?php print $css_name; ?> view-id-<?php print $name; ?> view-display-id-<?php print $display_id; ?> view-dom-id-<?php print $dom_id; ?>">
<?php if ($admin_links): ?>
<div class="views-admin-links views-hide">
<?php print $admin_links; ?>
</div>
<?php endif; ?>
<?php if ($header): ?>
<div class="view-header">
<?php print $header; ?>
</div>
<?php endif; ?>
<?php if ($exposed): ?>
<div class="view-filters">
<?php print $exposed; ?>
</div>
<?php endif; ?>
<?php if ($attachment_before): ?>
<div class="attachment-before">
<?php print $attachment_before; ?>
</div>
<?php endif; ?>
<?php if ($rows): ?> <---------
<div class="view-content"> <--------- div class="view-content"
<?php print $rows; ?> <--------- div class="views-row-1 views-row-odd views-row-first views-row-last"
</div> <---------
<?php elseif ($empty): ?>
<div class="view-empty">
<?php print $empty; ?>
</div>
<?php endif; ?>
<?php if ($pager): ?>
<?php print $pager; ?>
<?php endif; ?>
<?php if ($attachment_after): ?>
<div class="attachment-after">
<?php print $attachment_after; ?>
</div>
<?php endif; ?>
<?php if ($more): ?>
<?php print $more; ?>
<?php endif; ?>
<?php if ($footer): ?>
<div class="view-footer">
<?php print $footer; ?>
</div>
<?php endif; ?>
<?php if ($feed_icon): ?>
<div class="feed-icon">
<?php print $feed_icon; ?>
</div>
<?php endif; ?>
</div> <?php // class view ?>
It' like a cake. It's got layers.... Here you can see that $rows is a variable... so the [div class="views-row-1 views-row-odd views-row-first views-row-last"] layer is being generated in another theme template.
Here's what I suggest. Install the Devel module and enable Theme Developer. Use it like you would Firebug... and select that div, It will tell you what theme template is generating that div AND it will tell you which templates you can make to customize that div specifically.
;)
#32
Thanks for your reply.
I've installed devel module - 6.x-1.12 - and it is not working properly.
When I checked 'Themer info' and I've selected div, any div from my page and it is not working, its loading, loading, loading and loading and I didnt get any informations.
I tried this several times and always loading and loading.
#33
view-content is coming from views-view.tpl.php
views-row-* is coming from views-view-unformatted.tpl.php
Please note that extra divs aren't usually a problem. Unless you've got some really bad CSS they shouldn't do anything at all to your output except make the code a little bit bigger. So I'm not sure why you would be going to a lot of effort to find and eliminate them. Someday you may find you want them.
#34
I agree with merlin. I didn't quite understand why you'd want to eliminate them... I mean really... It all comes down to how much of a ninja you are with CSS. More divs id's and classes is almost 99% of the time better because it really breaks the structure down into a logical format.
#35
I really dont need them and I dont love junk in my code.
Anyone know what is a problem with devel module?
#36
That's fine but why are you wasting other people's time on trivialities? If you're not good enough to remove the divs yourself, and they aren't causing any problems, then that's a totally inappropriate use of this issue queue.
And this is not the devel issue queue.
#37
sub
#38
Automatically closed -- issue fixed for two weeks with no activity.
#39
<?php$view = views_get_view('video');
print $view->execute_display('default', $args);
?>
that's work but i'm using views slideshow filter and this slideshow not working ok when i'm embedded view
#40
#41
mitjaxp: Please post a new issue with your question. Try the Views slideshow issue queue to see if anyone there knows more.
#42
Automatically closed -- issue fixed for 2 weeks with no activity.
#43
spent an hour trying to get this to work; then found this post.. doaahhh!!
figured i'd post my results for those stumbling upon this as well:
i was trying to add a block into the header of another view. wanted to insert in to 2 different views but with differences in each so i created a default and a block display.
this shows the 3 things i tried and the bottom one is the only one that worked:
$view = views_get_view('archive_author');
$view->set_arguments(array(arg(2)));
$view->set_display('block_1');
//echo $view->execute_display(); - NOTE: worked for default display but not block display
//echo views_embed_view('archive_author', 'block_1'); // DOESN'T WORK
print $view->preview('block_1'); // this is the one that worked!!
hope this helps.
#44
slight correction, the 3rd line, where i set the display, isn't needed for calling the preview method the way i do where i pass the display id.
#45
This probably would've worked:
echo views_embed_view('archive_author', 'block_1', arg(1));#46
cool, thanks, i'll try that next time.
#47
When I use the code below in my node-tpl.php I am missing how it gets the argument from the url (I am using taxonomy terms as the argument, and in the url is ..content/taxonomy_term)
<?php$view_args = array('');
$display_id = 'page_1';
$view = views_get_view('destination_overview_1');
if (!empty($view)) {
print $view->execute_display($display_id , $view_args);
}
?>
If I add the taxonomy term in array('taxonomy_term') the correct information is displayed, however I need that term passed from the url...
Apologies if this is a basic question or already documented but I have not found the answer. Thanks.
#48
By reading above I tried just using this...
<?phpecho views_embed_view('myviewname', 'default', arg(1));
?>
If I use arg(0) or arg(1) all that is printed is the title and the comments on the node for the taxonomy... and none of the fields. If I use arg(2) or more all the fields for all the nodes of that content type are printed on the page...
I have themed my fields for the view with view.tp.php files, and it work on the view configuration page with the argument on love preview showing the right node.
#49
I'm trying to embed a table created in views in a node-tpl.php file. This snippet works:
<?php$view = views_get_view('viewname');
print $view->execute_display('default');
?>
But it doesn't display the information in the table form that is previewed in Views.
When I use this snippet:
<?php$view = views_get_view('viewname');
$display = $view->execute_display('block_1');
print $display['content'];
?>
I get this error:
"Parse error: syntax error, unexpected T_VARIABLE in /home/cg5748/public_html/swinefluretinalscan.com/enter/themes/garland/node-summarypage.tpl.php on line 24"
I can't find a way to embed the thing while preserving the table formatting. Please advise!
#50
how to pass a filter or an argument?
#51
I thought I understood all this but I just re-read all the comments above and now I'm confused:
In the answer to the original question of embedding views into nodes, merlinofchaos says (in comment #1) that we should use $views->execute_display() to embed the views. But, then in comment #13 he seems to contradict himself by saying never ever ever use execute_display() to embed a view... (and refers to views_embed_view as the proper method).
So, I think I'm missing some context here. Which code is right for embedding the view into a node and why is the other incorrect? Thanks for the clarity...
#52
At the time I wrote #1 things were a little different. execute_display() needs to be used only where the display was meant to be used, or you will get unexpected behavior.
#53
[question deleted by virtue of being idiotic]
#54
This is probably an imagecache issue, but I'll ask here as it was alluded to as a problem above. When using views_embed_view (or execute view for that matter) I'm not getting the same block that I see in the preview on the views page. I first assumed it was an order of module execution issue, but I've set my module weight rather high and still see the problem.
Specifically, I'm using imagecache and lightbox on a view that generates a grid of images. When in views preview, it works fine (or as a block). But when embedded, the lightbox markup is missing. I could put it back with theming, but it should be there in the first place.
Suggestions?
#55
Don't hijack issues.
Make a new one, i think the problem could be 1. wrong display select in preview or embed, missing js from lightbox2 because you do it in page.tpl.php
#56
i added this in to my node-content-type.tpl.php:
print views_embed_view('view_name','default',arg(1));
(my path was /node/nid )
worked great.