Need some help with passing Node ID to a view that's attached to a node teaser. The view needs to be filtered by the Node ID of the node that's in the teaser. I can't seem to do this with the Node ID from URL argument. Is there custom PHP code I can enter in the arguments settings for this ?

Comments

nevets’s picture

How are you attaching the view to the node teaser?

sid16’s picture

I'm using Display Suite to place the views block in my node teasers. I can still pass arguments to the view this way, it works on full nodes. I need to do the same thing for teasers.

nevets’s picture

In Display Suite under "Node Displays", under "Fields", add a new "Add new code field".

For the code try something like

<?php
// Change $view_name, $display_name and $args as needed
// The machine name of the view
$view_name = 'your_view_name';
// The display name
$display_name = 'default';
// The arguments to pass to the view in same order as defined in the view
// Here we pass just a node id
$args = array($object->nid);

$view = views_get_view($view_name);
if ( !empty($view) ) {
  return $view->preview($display_name, $args);
}
else {
  return '';
}
?>

Then add this field to the teaser display for the content type.

sid16’s picture

Ok, I tried that, but it still doesn't work. The view is displaying all the values. Is there any other way to pass the node ID to the view ?

nevets’s picture

Does the view expect a node id (nid) as an argument?

sid16’s picture

can you explain that please? only the node id argument would ensure the values in the view match that of the node in the teaser, right?

To explain what I want to do, here's an example. Let's say I have two nodes, called 'Fruit' and 'Vegetable.' When viewed as teasers (like on a taxonomy page or search results), the teaser for 'Fruit' has a block view that displays apples, strawberries, etc. The teaser for 'Vegetable' shows carrots, onions, etc.

nevets’s picture

It sounds like you want a view to list all the content of a certain type. If that is the case you could

a) Make a view that takes Node: Type as a view.

b) Make two views (or one view with two blocks) that has a Node: Type filter.

The later may prove easier for what you want.

nevets’s picture

An alternate approach is to use the View Field module.

sid16’s picture

Thanks, nevets! Viewsfield did the trick.