Hello everyone,

I 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!

Comments

merlinofchaos’s picture

Try this:

$view = views_get_view('viewname');
print $view->execute_display('default', $args);
merlinofchaos’s picture

Status: Active » Fixed
bensnyder’s picture

merlin I absolutely love you! thanks! and GREAT work on views!!! The UI has made leaps and bounds!

bensnyder’s picture

Status: Fixed » Postponed (maintainer needs more info)

merlin, check this out: (its my testing site)

pegleglax.homeip.net/sertoma

see the error views creates?

merlinofchaos’s picture

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.

bensnyder’s picture

Status: Postponed (maintainer needs more info) » Fixed

works!!!! Thanks!

<?php
$view = views_get_view('viewname');
print $view->execute_display('default');
?>
Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

vj0914’s picture

what should i do if I want to embed 2 views on same page?

vj0914’s picture

I figured out, Thanks

Matthew Davidson’s picture

And just to elaborate, you can change 'default' to whatever display id you want ('page_1', 'block_1', 'block_2'), eg.:

  $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.

deviantintegral’s picture

Version: 6.x-2.0-alpha5 » 6.x-2.0-rc1
Status: Closed (fixed) » Active

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

Matthew Davidson’s picture

Status: Active » Closed (fixed)

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.

merlinofchaos’s picture

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.

deviantintegral’s picture

Thank you - calling views_embed_view() is working perfectly!

For reference, my snippet:

$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

merlinofchaos’s picture

I think you'll find that the first part of all that can be eliminated by using the arg() function.

arg(1) perhaps.

deviantintegral’s picture

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

merlinofchaos’s picture

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.

Archie Bunker’s picture

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.

$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

gausarts’s picture

Version: 6.x-2.0-rc1 » 6.x-2.0-rc3
Assigned: bensnyder » Unassigned

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.

deviantintegral’s picture

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

mooffie’s picture

In the meantime, I have two options:
1. Two different blocks with each own Items to display.
[...]
But I considered both tedious.

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().

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

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.

gausarts’s picture

You are right. Thanks for the light.

fit’s picture

I embed my view with this code:

  $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?

bensnyder’s picture

...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,....
fit’s picture

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

						$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?

bensnyder’s picture

Assigned: Unassigned » bensnyder
Status: Closed (fixed) » Fixed

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.

bensnyder’s picture

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!!!

fit’s picture

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?

bensnyder’s picture

please copy/paste the HTML here so I can take a look at it... and comment on which divs are frustrating you

fit’s picture

div class="view-content"
div class="views-row-1 views-row-odd views-row-first views-row-last"

Those divs are frustrating me.

bensnyder’s picture

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.

;)

fit’s picture

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.

merlinofchaos’s picture

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.

bensnyder’s picture

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.

fit’s picture

I really dont need them and I dont love junk in my code.

Anyone know what is a problem with devel module?

merlinofchaos’s picture

I really dont need them and I dont love junk in my code.

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.

theabacus’s picture

sub

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

mitjaxp’s picture

$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

mitjaxp’s picture

Status: Closed (fixed) » Active
blackdog’s picture

Status: Active » Fixed

mitjaxp: Please post a new issue with your question. Try the Views slideshow issue queue to see if anyone there knows more.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

liquidcms’s picture

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.

liquidcms’s picture

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.

merlinofchaos’s picture

This probably would've worked:

echo views_embed_view('archive_author', 'block_1', arg(1));
liquidcms’s picture

cool, thanks, i'll try that next time.

tjb74’s picture

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)

  $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.

tjb74’s picture

By reading above I tried just using this...

echo 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.

SuperRookie’s picture

I'm trying to embed a table created in views in a node-tpl.php file. This snippet works:

$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:

  $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!

leovarg’s picture

how to pass a filter or an argument?

Rob_Feature’s picture

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...

merlinofchaos’s picture

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.

brynbellomy’s picture

[question deleted by virtue of being idiotic]

Silicon.Valet’s picture

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?

dawehner’s picture

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

dravidian7’s picture

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.

CccXxx’s picture

@#43 - liquidcms
Thank you very much. Your example was the only one to work for me. ty ty

Marko B’s picture

Currently if i use

$view = views_get_view('Banner_miss');
print $view->execute_display("block_1");

i only get Array returned, using this in node-something.tpl.php, is this still working or what is needed now to have view displayed?

merlinofchaos’s picture

When embedding a view, use $view->preview(), not $view->execute_display().

Rob T’s picture

Thanks for posting this, liquidcms. I was having some trouble getting my block display view to show in a node.

RichieRich’s picture

I just can't get this to work. No matter what I replace 'default' with I always get the default view. For example, I'm trying to use a block which displays only 5 nodes here but I still get the default view:

<?php
$view = views_get_view('Beaches');
$view->set_arguments(array(arg(2)));
$view->set_display('block5');
print $view->preview('block5')
?>

I've tried all the variants above but nothing seems to work. I must be making a mistake somewhere.

dawehner’s picture

block5 is not a valid display name. i guess it's block_5

BeaPower’s picture

Where do I put these codes?

awm6392’s picture

Status: Closed (fixed) » Active

I've used the following code to embed my view:

<?php
$view = views_get_view('seminarsview');
print $view->execute_display('default');
?>

However, now I would like to display each row of the view in a separate tab on the page. What code can I use to just embed a single row from a particular view, instead of just the whole view?

dawehner’s picture

Assigned: bensnyder » Unassigned
Status: Active » Closed (fixed)

This is a total different question.

You need definitive quite some custom coding to achive this.

In general hijacking issues is not nice.

yaworsk’s picture

subscribe

awm6392’s picture

Sorry. I gave it quite a bit of thought before posting and thought that the question was related enough to the original issue to be a part of this thread. I guess I was wrong, so I will post a new issue.

glennnz’s picture

I've tried all of these different options, and can only get the view content to display. The Title never shows...

glennnz’s picture

Version: 6.x-2.0-rc3 » 6.x-2.11
Status: Closed (fixed) » Active

Just changing the status and version.

dawehner’s picture

Status: Active » Closed (won't fix)
I've tried all of these different options, and can only get the view content to display. The Title never shows...

This depense on the display plugin you use. But i don't know one beside views attach which displays the title on the same region than the view.

Please don't hijack issues. Noone want's to read long issues with ten total different questions in it.

anne-pierre’s picture

thank you merlinofchaos. this worked for me. VIEWS is really great (once you get the hang of it)!

mossill’s picture

Thank you my friend!

how does this method interact with drupal's basic caching ? Is it making a database call every time a view is requested ?

henriquefm’s picture

What about Drupal 7?
views_embed_view() seems to not exist anymore....

dawehner’s picture

Why do you think this? It's definitive there

./views.module:1373:function views_embed_view($name, $display_id = 'default') {
henriquefm’s picture

rookie mistake.... I was only looking in *.php files... thanks very much

RichieRich’s picture

I still can't embed any other display than the 'default' view. What exactly is the display ID? If I create a view which contains a block called 'i am a block' is this the displayID? Surely it can't be simply 'block' because it's possible to create multiple blocks within a single view.

I suspect the problem is that I'm not specifiying the correct ID...but where is the display ID found?

RichieRich’s picture

Found out how to identify the display ID. Thanks to this link:

http://www.pridedesign.ie/content/drupal-get-view-display-id

frozen10’s picture

Thanks fit #25..worked great. I tried some others, but couldn't get it to display! just what I needed. Now I have to style it.

tmwagner’s picture

Finally... a thread that addresses the issue of embedding a view that is accurate! The beauty of simplicity!

Jerome F’s picture

If you're just trying to embed a view in a view as I was: http://drupal.org/project/views_field_view

rfiertek’s picture

Hi All,

I've got the view embedded, but for some reason, my links do not work inside the view. The links work elsewhere on the page. Any ideas how to fix this or get around it?

Thanks,
Rob

kerios83’s picture

<?php
$block = module_invoke('views', 'block_view', 'related_content-block');
print render($block);
?>

<?php
$view = views_get_view('related_content');
print $view->execute_display('default');
?>

<?php
$view = views_get_view('related_content');
print $view->preview('default');
?>

view name is RELATED_CONTENT (machine name. And in first example u need to add what is inside view -> Advanced ->
Other -> Machine Name: block (block or page by default).

So we got view inside a module (d7) but how to print view title ?

manoloka’s picture

I'm trying to embed the a view in a view's footer.

All the examples here worked on nodes but none works in a view ???

how do I do that?

dunx’s picture

Just because it took me a few minutes to work it out...

The $args array is the args taken by the view itself. So, if you have a view that takes the nid as it's only arguement, from within your tpl file you can call

      <?php
        $view = views_get_view('your_view');
        print $view->execute_display('default',array($node->nid));
      ?>
manoloka’s picture

Not sure if #84 was for me, if it was just let you know that doesn't work for me.

Thanks

manoloka’s picture

Finally I got it ... thanks to Kerios83 #82 post

I used this snippet

<?php
$view = views_get_view('my_view_name');
print $view->execute_display('page_1');
?>

Thanks to everyone ... Great job guys :)

juanjo_vlc’s picture

what about setting custom template suggestions?

For example, I wan't to embbed a view, but use one row tpl or another based on some parameter. How can I do that?

marcopanichi’s picture

Block from taxonomy view with title?

use this:

$view = views_get_view('view_machine_name');
$display = $view->execute_display('display_machine_name');
if( $display['content']!='' )
{
	echo "<div class='someclasses'><h2>Title</h2>".$display['content']."</div>";
}
Anonymous’s picture

In comment #13 the module author himself states:

Never ever ever use execute_display() to embed a view. Always use preview(). See views_embed_view() for the canonical example.

So why are people placing all these examples with execute_display?

Doesn't make any sense. Am I missing something?

thumbslinger’s picture

here here. I'm wondering the same thing. Shouldn't need a module for this. Should just be the right syntax of a snippet of php.

thumbslinger’s picture

Issue summary: View changes

fixing typo