Posted by Grzegorz Zbiński on October 8, 2011 at 1:02pm
I have a node type view default display with 2 args:
one is field with node reference, returning nid from referenced node,
second is nid which has exclusion
It works perfect, when added via panels.
I want to embed it in my tpl, and I got some problem:
<?php
print views_embed_view('my_view','default', $node->field_my_ref_field[0]['nid'], $node->nid);
?>is not working.
I've tried also (like in views ui preview):
<?php
print views_embed_view('my_view','default', $node->field_my_ref_field[0]['nid'].'/'.$node->nid);
?>but no luck - error.
<?php
print $node->field_my_ref_field[0]['nid'];
?><?php
print $node->nid;
?>I suppose, that there is some silly syntax mistake I made...
Comments
Hello, I usually do a
Hello,
I usually do a views_get_view then a $view->preview.
I believe views_embed_view is just a wrapper around that functionality anyway.
This page has apretty good explanation:
http://mydrupalblog.lhmdesign.com/embed-drupal-views-using-php
in case that site is down:
<?php$args = array(ARGUMENTS);
$view = views_get_view('VIEWNAME');
print $view->preview('display_id', $args);
?>
Thanks, but...
I did it your way
<?php$args = array($node->field_my_ref_field[0]['nid'],$node->nid);
$view = views_get_view('my_view');
print $view->preview('default', $args);
?>
Unfortunately no result.
Again: print_r($args)
Array ( [0] => 3139 [1] => 6182 ) - ok
But view is not being rendered.
The correct
This way, i always use. should be work fine.
<?phpprint views_embed_view('my_view','default', $node->field_my_ref_field[0]['nid'], $node->nid);
?>
You could try looking for other errors
As I wrote above
This snippet was first, I've tried (see at the beginning of post).
<?phpprint views_embed_view('my_view','default', $node->field_my_ref_field[0]['nid'], $node->nid);
?>
After jludwig comment I've tried even "non-wrapping" set of functions - no results.
I can see:
<?phpprint $node->field_my_ref_field[0]['nid'];
?>
I get 3139 - id of ref node
<?phpprint $node->nid;
?>
I get 6182 - id of node being viewed.
Another hint: I've set empty text "test nothing" to be sure, that view is rendered.
I see it, when embedding from snippet. Expected result I get in views UI in default preview section, when I put there args 3139/6182...
So snippet function gets necessary args, view works fine in views UI but running snippet in tpl results views empty text "test nothing" instead of this seen in views UI.
Mystery. I'm still confused.
Guys, don't shoot me!
After long night of coding, switching between test and production sites, I didn't notice that I was embedding code on test site, and checking results on production. :) I know, terrible mistake, but if someone is coding instead of sleeping it happens.
All solutions described above finally works fine (my_view was date related, and test site has no new records, test db branch was made earlier...)
So: don't shoot, mercy!
BTW I really don't know, witch method of embedding is more effective: view_embed_views or views_get_view and print. Or maybe it is not so relevant?
shoots Grzegorz Just kidding!
shoots Grzegorz
Just kidding! We all do that sometimes.
I really don't think it matters too much..
<?php
/**
* Embed a view using a PHP snippet.
*
* This function is meant to be called from PHP snippets, should one wish to
* embed a view in a node or something. It's meant to provide the simplest
* solution and doesn't really offer a lot of options, but breaking the function
* apart is pretty easy, and this provides a worthwhile guide to doing so.
*
* Note that this function does NOT display the title of the view. If you want
* to do that, you will need to do what this function does manually, by
* loading the view, getting the preview and then getting $view->get_title().
*
* @param $name
* The name of the view to embed.
* @param $display_id
* The display id to embed. If unsure, use 'default', as it will always be
* valid. But things like 'page' or 'block' should work here.
* @param ...
* Any additional parameters will be passed as arguments.
*/
function views_embed_view($name, $display_id = 'default') {
$args = func_get_args();
array_shift($args); // remove $name
if (count($args)) {
array_shift($args); // remove $display_id
}
$view = views_get_view($name);
if (!$view || !$view->access($display_id)) {
return;
}
return $view->preview($display_id, $args);
}
?>
As you can see, views_embed_view is just a wrapper around $view->preview. It makes a few more function calls, so it doesn't perform quite as well, but the difference is minimal.
But it is always few more lines of code...
...so right now I will be using view_embed_views due my personal comfort of reading the code.
I'm bullet proof :)