title kind of is self explanatory, this functionality is in the New comments field of the view, but you can't link to the comments from just the comment count.

Example text would be

Comments: (1)

and have that whole line be click-able to take you to the comments.

If there is a way to do this, please let me know.

Comments

Equ’s picture

I'd be interested in this too.

Daugilas’s picture

I am also very interested in this. But it is opened almost for two months and nobody solved this??

Daugilas’s picture

Well I found a huge and terrible workaround...

What I did was:

  • In my view I put "Node: Comment count" and "Node: New comments" fields;
  • I configured my "Node: Comment count" and checked "Exclude from display"
  • In "Node: New comments" I checked "Link this field to new comments";
  • In "Node: New comments" I checked "Rewrite the output of this field" and put "[comment_count]" as a text;
  • In "Node: New comments" remember to uncheck "Display nothing if no new comments";
  • The only thing now is that it displays "node/bla_bla_bla#new" and we want "node/bla_bla_bla#comments";
  • So here is the worst thing in this workaround: open
    <..>\modules\views\modules\comment\views_handler_field_node_new_comments.inc @line 81
    and change that line from this:
    $this->options['alter']['fragment'] = 'new';
    to this:
    $this->options['alter']['fragment'] = 'comments';

And thats it. It works! :) but its BAD. I do not recomend to do that.

There is a way to add some code to views so you get "Link this field to comments" checkbox under "Node: Comment count" I just hadn't time for that. And as I do not use "Node: New comments" anywere in my views it works for me now...

fei’s picture

subscribing/searching

Michael Fuchs’s picture

There's a less horrific work-around - if you don't mind slogging through the 55,000-some variables set in the context of the running script. I'm relatively new to Drupal, so I can't vouch for the universal applicability of this, but:

$variables['view']->result[$count]->nid

holds your node id. (Where $count is a number representing the row in your array of displaying results - i.e 0 through N, with N being the last record.)

So, if your template is looping through the results like mine is, ie:

<?php foreach ($rows as $count => $row) {

then you can add the line:

$my_nid = $variables['view']->result[$count]->nid

Then you can use that later to construct your link. Obviously, in Views, don't output it as a link at all. And in your template, put:

<a href="/node/<?=$my_nid?>#comments"><?php print $rows[$count]['comment_count'] ?> comments</a>

As a more general matter, you can get access to all the variables in the scope of the script with this:

$varList = get_defined_vars();
print('<pre>');
print_r ( $varList );
exit();

if you don't run out of memory before they all display. (Now I know why Drupal is such a memory hog.)

Cheers.

samwich’s picture

I've upgraded to the newest version of views and found that this is possible to do.

Add a new field that is Node:Node ID
choose exclude from display
move this field above your comment count field
in your comment count field check the box "output this field as a link"
in the link path type in /node/[nid]/#comments
you can delete the text that is in the label
add Comments: into the prefix
update your display
save your view

dudenhofer’s picture

rock on! you're a life saver, I'll be using this a lot

bluepen-1’s picture

Hey! Thanks a lot for this. Got me to upgrade my Views module :p

Something out of topic: I've been trying but couldn't figure out how to hide comments if there are none. Looks ugly with a big fat "0".
My comment count appears in my views, so I tried added to my views-view-fields--x.tpl.php:

if ($fields comment_count > 0) {
echo "<div id=\"comment_count\">".$fields['comment_count']->content."</div\n>";
}

but that didn't work. Tried

if ($fields['comment_count']->content > 0) {
echo "<div id=\"comment_count\">".$fields['comment_count']->content."</div\n>";
}

but that didn't work either.

I think it's something fundamental, so if someone could give me some pointers I'd be very grateful.

Thanks!

jeni_dc’s picture

The method in comment #6 works very well but if you are using path aliases then the links will be output as:

your-site.com/node/3#comments

instead of

your-site.com/path-alias#comments

I often use custom templates for my views to theme them. Then you can output the link with the l function like so:

<?php print l($fields['comment_count']->content, 'node/' . $fields['nid']->content, array('fragment' => "comments"));?>

I always use pathauto on my sites for SEO, so this keeps things consistent, avoids duplicate urls, etc.

siteograf’s picture

Delicious Creative were did you used this code? in "Field Node: Comment count" views template?
From what views template files I can access $fields['comment_count']->content and $fields['nid']->content variables?

Thank you for the answer!

jeni_dc’s picture

I used that code in the template for the Row style output, so if your view is called "blog", this would go into a template named views-view-fields--blog.tpl.php

Since this combines the output of a couple of different fields this isn't something you would do in a template for one specific field.

Hopefully this helps answer your question! Let us know how it works out.

NPC’s picture

You can also do it in the view fields itself - by adding a Customfield PHP code, and using this as the value (providing the "comment count" field is included in the view):

print l($data->node_comment_statistics_comment_count, 'node/' . $data->nid, array('fragment' => "comments"));

You can check the actual name of the comment count field in the SQL text of the view.

It worked for me, thanks to Delicious Creative for the tip!

Helmut Neubauer’s picture

To #9
This doesn't work for me. I tried to add the nid field to my view and to exclude it from display (and I put it at the top of the fields). Then the link will be displayed without the node id. If I don't check "exclude from display" it works, but then Drupal prints the node id. Is this behaviour normal? I'm not sure.

As workaround I can ignore the field in the loop in the template file, but I thought the way described above has to work. Can somebody help me? Thanks.

jeni_dc’s picture

HI Helmut,

If you are going to be using the template method in comment #9, instead of the method in comment #12, then you cannot exclude the NID field from display. In fact it's very necessary! If you check into the code I posted then you'll see that the field is output:

$fields['nid']->content

In the end what you want to do with the template file is manually rearrange the way that the fields are being printed, and reformat them into the code snippet above.

When you say "then Drupal prints the node id" where is that being printed? If you completely rewrite the template file to print things where you want, then nothing that you don't explicitly print will be output in the view.

jumoke’s picture

This worked for me, thanks

dawehner’s picture

Status: Active » Fixed

So its fixed

Status: Fixed » Closed (fixed)

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

somanyfish’s picture

The node group under fields has an option to choose "Node:Path". This outputs the path updated by PathAuto, so you get the nice alias. I followed #6, except I used Node:Path instead of writing my own path via Node:NID and this worked great. Make sure to check "Use absolute link (begins with "http://")" in the settings for Node:Path, so that you get the full URL when you need it for Node:Comment Count.

I'm glad there's a way to keep all of the settings in the view, instead of setting it up in a template.

NPC’s picture

somanyfish, wow, using Node:Path is such a simple solution! Not sure how I missed it earlier - was this token added just recently to Views?

jeni_dc’s picture

Yeah that is a better solution! NPC, it does look like it was added to Views at some point. I just checked an older site, and one I'm working on right now, the older site running 6.x-2.6 doesn't have Node: Path, where the newer one running the current 6.x-2.11 does. Just another reason to keep your sites up to date, shame on me! ;-)

topdawg’s picture

Regarding http paths with clean urls and some of the above comments, all of which can be done entirely within views and no custom codes nor php additions.

In my case, for instance, the path link had to be "node/[nid_1]/[comment_count]" WITHOUT the preceding slash for root (nor the quotes of course.) The field replacement types are given within the view interface and generated as replacement options to use, so pay attention to yours
it looks ike this-

Replacement patterns

The following substitution patterns are available for this display. Use the pattern shown on the left to display the value indicated on the right. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.
Fields

* [title] == Node: Title
* [nid_1] == Node: Nid
* [comment_count] == Node: Comment count
* [created] == Node: Post date
* [name_1] == User: Name
* [body] == Node: Body
* [comment_count] == Node: Comment count

This could be a version of views and drupal whereas mine is drupal 6.19, php 5.2.13, and Views 2.11 (Jun 16, 2010)

- Joe

NPC’s picture

topdawg, does it output a clean path, though?

I would expect it to output something along the lines of node/9/... - which is very SEO-unfriendly. Using Node:Path, as described in #19, is a very good solution, so I don't think any other one is needed.

topdawg’s picture

No, not clean, like you say node/## but I didn't think much of it for the comments. In any case I do not see the [path] as a replacement option no matter how I choose it. How did you get it to work?

topdawg’s picture

Never mind, I moved the field up above the comment count field and now the [path] shows up as a substitute as follows:

Fields

    * [title] == Node: Title
    * [nid_1] == Node: Nid
    * [path] == Node: Path
    * [comment_count_1] == Node: Comment count

(BTW I have two links, above and below which is why you see [nid_1] and [comment_count_1]

uno’s picture

My scenario, used for fields populated Frontpage View:

Added field "Node:Path", excluded it from display.
After that added "Comment count" field, choose Rewrite the output of this field (since I do not want just the comment count digits to be linked), added Comments: [comment_count] there, than choose Output this field as a link, added [path]#comments respectively.

Voilà, I have both the text and comment count comments: n linked to a comment section of a parent node, using pathauto URL, almost the same comment functionality as it is with the Node Row Style (or default frontpage).

Only thing I am missing now are the plural forms.