I have comments on nodes that are categorised by terms. I need to show a view of the terms and if there are any new comments in any of the nodes that term is tagged with.

So if I have the following:

-NodeA (has new comments)
-NodeB (has new comments) 
-NodeC (no new comments) 
-NodeD (no new comments) 
-NodeE (no new comments) 
-NodeF (has new comments)

I need my view to look like this:

Term1 – new comments!
Term3 – new comments!

First I tried making a view of comments to include the new comments field and using relationships to show the terms. However if a term has no nodes or comments (Term4 in my example) I need the term to still be displayed.

So I then tried making a view of the terms. I added the relationships 'Taxonomy term: Content with term' and the field 'Content: New comments' which uses this relationship. This does show the number of new comments as expected. It gives me duplicated results (which I may be able to fix with grouping fields) but unexpectedly it also hides the terms that have no comments or nodes.

Am I going about this the right way and if so how can I stop the 'empty' terms being hidden?

UPDATE Ive updated D core and a few modules that had new versions (but not views). The view works fine with the relationship of 'Configure Relationship: Taxonomy term: Content with term' when the field is 'Content: Title (Title)'. However when I add the field 'Content: New comments' then terms that have no comments are hidden.

UPDATE Ive tried the following in a views field:

if(intval($data->node_new_comments) > 0) {
  if(!array_key_exists($data->tid, $static)) {
    print "new comments !";
    $static[$data->tid] = 1;
} else {
  print "here you do whatever you want when there's no new comment";

UPDATE - by 'new comments' I mean comments that the logged in user hasnt read. This is a standard field in views. I need this rather than an arbitrary date cut off. Sorry for any confusion.

  • Are the terms still hidden if the views relationship is set to not required? Dec 31, 2012 at 6:05
  • Here's the debugging things I'd look into if it was me, answering these may help someone answer this: 1) Do the terms re-appear on removing the New Comments field, the relationship, and/or both? 2) What happens when you use Term Name as a grouping field, or, add Term Id and use it as a grouping field? 3) 2) Is the term name field definitely Taxonomy term: Term name and not Content: -term vocabulary- (easy mistake to make!) 4) Do you see the same thing if you create a new Taxonomy Terms view from scratch with only the relationship, Taxonomy Term: Term Name and Content: New Comments? Jan 2, 2013 at 0:27
  • which version of drupal are you working with? with which version of views? are you working with relationship for a particular reason? did you try the "group by" option instead?
    – pasine
    Jan 2, 2013 at 14:06
  • @notme im using the latest drupal and modules. As I have a view of terms I need to use a relationship to get to the field that shows if the nodes have new comments.
    – Evanss
    Jan 2, 2013 at 15:19
  • 1
    @notme, if the view is a list of nodes, then again -- terms that don't have nodes attached wouldn't be available, so the same issue would stand. jdln, not a perfect solution, but how about adding a new attachment to your view that only displays terms which have no nodes and attaching that display under your regular page view? Would that work? Then the terms with no nodes would always print out at the bottom, so sorting wouldn't be as easy -- but perhaps it's an option. Jan 2, 2013 at 15:40

4 Answers 4


This works for me. I have a term view that shows the term name field and my code adds the new comment flag after the term name.

The code goes in a custom module.

You need to change to VIEW_NAME to your view name and the TAXONOMY_FIELD_NAME to the name of your field (like field_tags for example).

 * Implements hook_views_pre_render().
function MYMODULE_views_pre_render(&$view) {
  global $user;
  if ($view->name == 'VIEW_NAME' && $user->uid) {
    // Get all the tids of the views results.
    $tids = array();
    foreach ($view->result as $result) {
      $tids[] = $result->tid;

    // Get all the tids that have recent comments.
    // Only return terms that relate to nodes that have not been viewed by
    // the current user since the last comment.
    $query = db_query("SELECT t.TAXONOMY_FIELD_NAME_tid
                         FROM {node_comment_statistics} c
              LEFT OUTER JOIN {history} h ON c.nid = h.nid AND h.uid = :uid
                   INNER JOIN {field_data_TAXONOMY_FIELD_NAME} t ON t.entity_id = c.nid
                        WHERE c.comment_count > 0
                          AND t.TAXONOMY_FIELD_NAME_tid IN (:tids)
                          AND (h.timestamp IS NULL OR h.timestamp < c.last_comment_timestamp)",
               array(':uid' => $user->uid, ':tids' => $tids));
    $commented_terms = $query->fetchCol();

    // Loop through the results and add the new comments flag where necessary.
    foreach ($view->result as &$result) {
      if (in_array($result->tid, $commented_terms)) {
        $result->taxonomy_term_data_name .= ' new comments!';
  • By 'new' comments I mean comments that the logged in user hasnt seen. This is a standard field in views. Does your code use this, or is it an arbitrary date value? From your code comments it seems like the latter?
    – Evanss
    Apr 20, 2013 at 11:11
  • In addition to @jdln's comment above, history table contains data of read nodes by uid.
    – AKS
    Apr 20, 2013 at 11:54
  • It was using the last access time of the user, so it would show comments written since the user was last logged on. For logged out users it was using a fixed period of 2 weeks. I have now changed it so that it works only for logged in users and it is based on when the user last viewed the nodes that the comments are on.
    – rooby
    Apr 20, 2013 at 13:58

Adding the 'Content: New comments' field adds an INNER JOIN to the node_comment_statistics table, which can potentially be the source of the problems you're experiencing. (For example, if there's no entries for NodeC, NodeD and NodeE in node_comment_statistics for some reason, the terms will not be shown due to the JOIN being an INNER one.)

Does rebuilding node_comment_statistics help?

  • The reason im getting the extra results is I get a result for each node, as each node has a number value for the number of new comments. Its annoying that I cant just have a result for each term and a yes/no value for if there are any new comments on any of the nodes for that term, however I dont think this is a bug, its by design.
    – Evanss
    Apr 16, 2013 at 9:41

Here's a sketch of an answer that'll get you most of the way there, I think. Start by creating a view of nodes, then add the taxonomy and the latest comment time as fields.

Now turn on aggregation for the view, and set the aggregation for the taxonomy field to be "group results together", on the column "tid".

Change the aggregation setting for the comment time field to be "Maximum".

You should now have a view which lists all your taxonomy terms (at least those that have content) with the last comment date. The final step will be to process the comment timestamp in a views hook or theming to make it display as you require, which should be relatively simple.

  • How do I do the last step of 'Change the aggregation setting for the comment time field to be "Maximum".' ?
    – Evanss
    Apr 19, 2013 at 9:17
  • Once you have turned on aggregation settings, every field in the list under Fields should have an aggregation settings link next to it. Click on that and you should see a pulldown labelled "aggregation type". Maximum is one of the options. Apr 19, 2013 at 11:25
  • Ive done this but its not grouping the results per taxonomy term. I still have a result for each node.
    – Evanss
    Apr 19, 2013 at 15:21
  • Did you set the aggregation for the taxonomy field to be "group results together", on the column "tid", as per my answer? I have tested this method so I am pretty confident it works. Apr 20, 2013 at 16:18
  • 1
    I tried this approach out on a test install and got the results as described so there must be some difference with what we have each done. Did you you remove the node title field? You don't need it and it will mess up the result. Apr 23, 2013 at 8:19

You have to make another query first, to retrieve the terms which have nodes with new contents.

I've been helped by this thread.

As I told you, I put all this in php fields ; if you have the time, it'll be better to put it in a module, in a views_query_alter or else.

  1. Add a php field, and in the setup code, go grab the tids' list of terms which have new content :

    $query = db_select('node_comment_statistics', 'c')
      ->condition('c.last_comment_timestamp', 'history_user.timestamp', '>');
    $query->join('field_data_YOURTERMREFERENCEFIELD', 't', 't.entity_id=c.nid');
    $query->fields('t', array('YOURTERMREFERENCEFIELD_tid'))
    $static = $query->execute()->fetchCol();
  2. Now in the output code, check that your term is in this array :

    if (in_array($data->tid, $static)) {
      print 'new comments !';

This is the lightest I see, even if the query can be heavy if you have a large amount of nodes and comments.

EDIT: I have to recall that putting php in the sql with views php is NOT a good practice, I invite you to put all that in a custom module in hook_views_query_alter as soon as you can.

  • Im afraid its not working. Every term has the field of 'new comments !' even though only some actually have new comments on their content. The only exception is the term which doenst have any nodes at all.
    – Evanss
    Apr 15, 2013 at 18:13
  • @kiamlaluno edited the code. Does it work now ? It does for me :) Apr 23, 2013 at 10:57
  • I didn't change the code; I simply reformatted the text. :) If it doesn't work for @jdln, but it works for you, then it is possible you are applying the code differently, or the taxonomy terms are taken from different vocabularies (e.g. a tag taxonomy).
    – apaderno
    Apr 23, 2013 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.