I have a view setup that uses fields instead of nodes to basically display a list of node teasers. I'm committed to doing it that way because of other aspects of how the sight is set up. I want to display all of a node's comments under that node like it does when you're using "row style: node". The problem is that when using "row style: fields" it creates a new row for each comment so that if a node has 10 comments it will be displayed 10 times in said view. It seems like it wouldn't be too tough to do. Taxonomy is able to offer all terms associated with a node as a field. I apologize in advance if this has been answered already. I looked quite a bit and couldn't find anything. Thanks in advance for any responses.

Comments

browlands’s picture

how do you display in fields row style ...

node field 1
node field 2
comment body 1
comment body 2
comment body n

browlands’s picture

Use http://drupal.org/project/viewfield.

Create a view for comments, no need for arguments, just set up the fields as you wish.
Create a field type viewfield on the original node and use the default of the new comments view, including the argument nid. This field can be hidden on the display of the original node and teaser form.

Voila, this field, a listing of comments is available in views as a seperate field.

jvdurme’s picture

Thanks man, you're a lifesaver. ;-)
One issue remains: the comments of one node are also displayed in another node.
How can I make sure the comments are displayed only under the correct node?

Thanks!

Wakken!

jvdurme’s picture

Ok, I had to set the comments view to Node type and enter Node ID as argument.
Previously I had the view as Comment type and I couldn't enter the Node ID as argument (only comment fields were possible).

Wakken!

kscott22’s picture

Thanks for the suggestion -- it's a great one!

However, I think I'm doing something wrong with the arguments. ALL comments are appearing in EVERY row, rather than just all the comments for that node. Any suggestions? (I using Drupal 7.)

Thanks again!

kscott22’s picture

With Drupal 7, I just had to make some adjustments to the arguments. In the comments view, I added Content: NID as a contextual filter, and all was well. Thanks again!

xanderol’s picture

How did you configure your contextual filter? I have not been able to get this to work. I've tried to get it to work by providing a default value but since you can't pull the node id from the URL it would have to be done with PHP code but I can't seem to get anything to work.

kpastore’s picture

This works well for a normal view, but when using it with Views Data Export, the exported comments show up with "Edit, Export, and Clone" as well as a lot of line breaks in it. causing a very large spreadsheet cell. Any thoughts as to why this may happen? Thanks!

gregoiresan’s picture

This is not issue but it would be nice if we could have the "comment = node link" showing the same views as set in a default node view. like this we could use Ajax Comment, display extra-fields and show other fields the way we want without sacrificing the comment module which is great.

asif.mulla’s picture

Hi scalp,

Could you please explain all steps in detail?. I tried to do the steps given, but unfortunately I didn't get the comment value inside my view.

acy76’s picture

I was unable to get viewfield to work as I wished, but creating the comment view as outlined above (with an argument/contextual filter for the parent NID) allowed me to use views_embed_view along with hook_views_pre_render in a custom module to append the comment view as a post-view attachment:

/**
 * Implementation of hook_views_pre_render()
 */
function mymodule_views_pre_render(&$view) {
  if ($view->name == 'frontpg') { // choose which view to modify at the pre-render stage
    $_node_id = $view->result[0]->nid;
    if ($view->current_display == 'block' || $view->current_display == 'block_1') { // i am only modifying these particular displays; alter as needed
      $view->attachment_after = views_embed_view('name_of_view_to_embed', 'block', $_node_id);
      $view->attachment_after .= "<a id=\"comment-link\" href=\"node/$_node_id#comment-form\">Add a comment.</a>";
    }
  }
}

Please note that this is tested on D7 only - the $view object might differ under D6, so some modification may be required (although the concept should still hold).

So: what does this code do?

First, like many hooks, you'll have to use an if/switch to select the view you want to append comments to; in this case, it's the 'frontpg' view.

Since we'll need the node ID of the content being displayed in the view (my view only displays one item at a time), we pull it out of the view results. Multiple results could be dealt with in a foreach loop or similar.

Next, my view has two block displays I want to modify, so I am checking for them with an 'if' statement. If you want to modify all displays for your view, you could eliminate this check.

Then, I am simply adding two things to the view's attachment_after property - first, I am embedding my comment view with views_embed_view (see http://api.lullabot.com/views_embed_view for documentation of this function) and passing the NID of the content in the view so I only get the comments attached to that node. Then, I am creating a link to add a comment, again using the NID value, and concatenating that with the already attached view.

The views_pre_render hook is really quite powerful, and I hope this demonstrates at least one common use case.