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?
2 Answers
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;
}
}
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.
-
By 'new' I meant unread comments for that user. This is a filed available in views.– EvanssCommented Apr 20, 2013 at 11:09
-
$term
as a variable within this template, so you therefore have$term->tid
to pass to a View directly if need be.