By default, the "Node: Comment Count" field only displays the number without any text (or link to the node). The suffix field allows you to put the text "Comments" in it, however, the text "1 Comments" is grammatically incorrect.

While we can do this in the theme layer, it would ideal if we didn't need too:

function THEME_preprocess_views_view_fields(&$vars) {
  // pluralize the comment count
  if ($vars['fields']['comment_count'] && is_numeric($vars['fields']['comment_count']->raw)) {
    $vars['fields']['comment_count']->content = format_plural($vars['fields']['comment_count']->raw, '1 Comment', '@count Comments');
  }
}

Comments

dawehner’s picture

I'm thining about a views_handler_field_count field which extends the numeric fields:

It allows to define two strings in the data definition, which are used in format_plural.

Any oppinions about this?

kmonty’s picture

Honestly, I'm not a views developer but that sounds like a great idea.

rjbrown99’s picture

+1 for #1. Sounds like a good solution to me. Glad I found this issue. For now I'm heading the preprocess route as well.

halstead’s picture

I altered the snippet for my new comments. I also changed it to preserve the link views was outputting.

function THEME_preprocess_views_view_fields(&$vars) {
  // pluralize the new comment count
  if ($vars['fields']['new_comments'] && is_numeric($vars['fields']['new_comments']->raw)) {
    $new = format_plural($vars['fields']['new_comments']->raw, '1 new comment', '@count new comments');
    $vars['fields']['new_comments']->content = str_replace('>'. $vars['fields']['new_comments']->raw .'<', '>'. $new .'<', $vars['fields']['new_comments']->content);
  }
}
Letharion’s picture

Category: feature » task

@Dereine
I take your comment as "This would be useful in Views".

Moving this to the queue.

steven jones’s picture

So the patch in http://drupal.org/node/317653 got committed, would suggest that just needs to be ported to 3.x?

dawehner’s picture

@#7

Afaik this is commited to every branch.
Yes it it.

steven jones’s picture

So is it now possible to do what the op requested? And thus this issue can be marked as fixed/duplicate.

Letharion’s picture

Status: Active » Closed (duplicate)

What Steven said.

couturier’s picture

Version: 6.x-3.x-dev » 7.x-3.0-rc1

For the benefit of instruction, in 7.x-3.0-rc1, this function currently works as follows:

  1. Add a "Comment count" field
  2. Under configure field, check the "Format plural" box
  3. Input under "Singular form" your choice of words, for example, "View 1 comment"
  4. Under "Plural form," enter your choice of words along with the count command, for example,"View @count comments"
  5. Under "No results behavior," check "Hide if empty"
  6. Apply changes, save the View

For more information about how to turn the comment count into a link, see: Turning a Views comment count field into a link to comments.