0

I have a blog section on a bilingual page that I'm working on and I'd like to show the total number of comments below each teaser. Not to confuse users, the total should be total number of comments for the language that user is in. So, let's say if I have Spanish and English version of the website. If the user is on English, it should only show total number of comments in English not the grand total. Likewise, if the user is on Spanish, it should only show total number of comments in Spanish. Right now, it shows grand total and confuses users. How do I resolve this issue? Any help would be appreciated.

Drupal 7.x

1

This is untested but you could override the comment_count variable in a template_preprocess_node.

/**
 * Implements template_preprocess_node().
 */
function MYTHEME_preprocess_node(&$variables) {
  // Not sure if you wanted to limit to teasers but here goes.
  if ($variables['view_mode'] == 'teaser') {
    // Get the node object.
    $node = $variables['elements']['#node'];
    // Fetch the language the user is browsing the site in.
    global $language;

    // Query against the database all comments for the node in language the user
    // is browsing the site in.    
    $result = db_select('comment', 'c')
      ->fields('c', array('cid', 'language', 'nid'))
      ->condition('language', $language->language, '=')
      ->condition('nid', $node->nid, '=')
      ->execute();
    $comment_count = $result->rowCount();

    // Now we override the comment count from the comment module with our result.
    $variables['comment_count'] = $comment_count;
  }
}

Observe that Views for example fetches $node->comment_count which is set in a node_load by the comment module.

So if you are using Views to display the comment count will still be wrong. One idea could be to add your own hook_node_load and populate $node->comment_count. This needs to be done after the comment module has run it's hook_node_load.

But if you are displaying nodes using the teaser view mode the code above (maybe with some modifications) should work :)

Edit On second hand it might make more sense to setup a new variable called something like comment_count_active_language instead of overriding comment_count. In theory the comment_count is correct for the node. You are more asking for another count.

Change the example above but setup a new variable.

$variables['comment_count_active_language'] = $comment_count;

Then print that in node.tpl.php instead of $comment_count.

  • Your code seems working. I verified it with dpm and it does modified the comment count. However, for some reason, the count isn't displayed and grand total is still displayed instead. Seems that something is overwriting my changes. Do you know what could cause this? – KMC Jul 28 '14 at 19:36
  • @user30646 hmm. maybe you have another function overriding the variable? I've updated the answer with another way that might make more sense that adds a new variable instead of overriding. Don't forget to mark the answer as accepted if it was useful to you. – Pontus Nilsson Jul 29 '14 at 11:38
  • I couldn't figure out which function is overwriting the comment_count in the last minute. Or maybe it wasn't used at all. But I ended up rewriting the text stores inside ['content']['links']['comment']['#links']['comment-comments']['title']. Thanks for your help Pontus! – KMC Jul 29 '14 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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