0

Does anybody know how to get the nid from a node using contextual filters or filter criteria in views

WITHOUT using "Get id from url"? I was thinking about using PHP or getting it from a node-field.

My view is a block of comments that is to be placed together with a node. The comments needs to belong to the node and linked by node ID. In this case the "Get ID from url" wont work since the node itself is listed in another view.

It is a view-block (containing comments) inside of a node that is listed in a view.

3
  • I have added a bit more, does it make sense? :) @krister andersson
    – WebWebster
    Nov 24 '12 at 4:03
  • Do you use views_get_view in a template file to load and display your view? Nov 24 '12 at 4:08
  • I use the following to print the comments in the node template file <?php $block = block_load('views', 'comments-block'); $output = drupal_render(_block_get_renderable_array(_block_render_blocks(array($block)))); print $output; ?> Would another print-method work better? @krister andersson
    – WebWebster
    Nov 24 '12 at 4:11
3

If you are loading and displaying your view in the node.tpl or node--<content-type>.tpl file, you could supply the current node nid as an argument to your block view, then you can setup a contextual filter for Content: Nid. I would do something like this:

 // get view
 $view = views_get_view('YOUR_VIEW_NAME');
 // set display for view
 $view->set_display('YOUR_VIEW_BLOCK_NAME');
 // set current node nid as argument to view
 $view->set_arguments(array($node->nid));
 // execute view
 $view->execute();
 // render view
 print $view->render();

You could also try to use views_embed_view() which I think would be the preferred way to do this. It takes two parameters, name and display_id, any additional parameters will be passed as arguments to the view:

print views_embed_view('YOUR_VIEW_NAME', 'YOUR_VIEW_BLOCK_NAME', $node->nid);

I'm not sure that this helps, the question is a little vague=)

4
  • Im sorry if the question was badly formulated, but I like where the answer is going! I have used your code in the template-file of the node now, how to I let the "comments-view" to get the argument? My current view-setup : "View A" : Listing nodes "Node A" : Is part of "View A" and has "View B" printed inside. "View B" : Lists comments. Comments need to show based on the node ID so that the right comments show. This view is the one that needs to collect the argument. "View B" setup: Relationships: "Comment:Content" , "Cont.Filt:Comment:Nid" Did this clarify the situation?
    – WebWebster
    Nov 24 '12 at 4:40
  • It's a little hard to answer without knowing exactly how you have setup your view. If you export the view and email me I can have a closer look. Nov 24 '12 at 4:47
  • If View B is an attached view you should be able to pass the nid by setting Inherit contextual filters to true under Attachment Settings. Nov 24 '12 at 4:49
  • 1
    Your code was all that was needed, it did the trick and saved me hours, thanks a million!
    – WebWebster
    Nov 24 '12 at 4:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy