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
Comment #1
dawehnerI'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?
Comment #2
kmontyHonestly, I'm not a views developer but that sounds like a great idea.
Comment #3
steven jones commentedRelated 2.x issue: #317653: Add form fields for singular/plural for suffix/prefix on numeric fields
Comment #4
rjbrown99 commented+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.
Comment #5
halstead commentedI altered the snippet for my new comments. I also changed it to preserve the link views was outputting.
Comment #6
Letharion commented@Dereine
I take your comment as "This would be useful in Views".
Moving this to the queue.
Comment #7
steven jones commentedSo the patch in http://drupal.org/node/317653 got committed, would suggest that just needs to be ported to 3.x?
Comment #8
dawehner@#7
Afaik this is commited to every branch.
Yes it it.
Comment #9
steven jones commentedSo is it now possible to do what the op requested? And thus this issue can be marked as fixed/duplicate.
Comment #10
Letharion commentedWhat Steven said.
Comment #12
couturier commentedFor the benefit of instruction, in 7.x-3.0-rc1, this function currently works as follows:
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.