0

Is it possible to add some PHP to taxonomy-term.tpl.php that will look and see if any content tagged with the term has new comments, and if so print something in the html?

4
  • you have $term as a variable within this template, so you therefore have $term->tid to pass to a View directly if need be.
    – Jimajamma
    Commented Apr 15, 2013 at 15:39
  • I actually think embedding a view isnt the right route to go down. Ive removed that part of my question.
    – Evanss
    Commented Apr 15, 2013 at 15:55
  • Are you use Drupal 6 or 7? Commented Apr 15, 2013 at 19:40
  • Im using Drupal 7.
    – Evanss
    Commented Apr 16, 2013 at 9:56

2 Answers 2

0

Yes you can but it will it performance without proper caching. Don't do that in tpl because you can cache it, even with page cache. Code look something like that using drupal api.

<?php
$has_comment = false;    
$nids taxonomy_select_nodes($term->tid);
foreach($nids as $nid){
$node = node_load();
  if($node->comment_count > 0){
    $has_comment = true;
  }
}
0

You'll have to determine what you mean by "new" comments. Perhaps you mean comments posted within the last 24 hours? 48 hours? If so, then you would want write a preprocess function in your theme using template_preprocess_taxonomy_term(). Perhaps something like this:

/**
 * Implements template_preprocess_taxonomy_term().
 */
function demo_preprocess_taxonomy_term(&$variables) {
  $term = $variables['term'];
  $tid = $term->tid;
  // These are all the nodes that have the current taxonomy term.
  $referenced_nodes = taxonomy_select_nodes($tid, false);

  // Only keep going if there is at least one node that has the term.
  if (!empty($referenced_nodes)) {
    // Find comments posted within the last 24 hours.
    // @see comment_get_recent().
    $query = db_select('comment', 'c');
    $query->addTag('node_access');
    $query->fields('c')
          ->condition('c.status', COMMENT_PUBLISHED)
          ->condition('c.created', time(now) - 60*60*24, '<=');
    $comments = $query->execute()->fetchAll();

    // Only keep going if there is at least one comment that fits our criteria.
    if (!empty($comments)) {
      $comment_nids = array();
      foreach($comments as $comment) {
        $comment_nids[$comment->cid] = $comment->nid;
      }
      if (!empty($intersection = array_intersect($comment_nids, $referenced_nodes)) {
        // Add all the comments in $intersection to something like $variables['comments'].
        // Then in your taxonomy-term.tpl.php, you can check if $comments is empty. 
        // If not, then render the comments.
      }
    } 
  }
}

Note that I have not tested this code, so please treat it as pseudo-code that you'll need to tweak.

2
  • By 'new' I meant unread comments for that user. This is a filed available in views.
    – Evanss
    Commented Apr 20, 2013 at 11:09
  • I see. Sorry my answer was off topic!
    – rocket
    Commented Apr 20, 2013 at 16:35

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.