You are here

function _socialbase_node_get_comment_count in Open Social 8.8

Same name and namespace in other branches
  1. 8.9 themes/socialbase/socialbase.theme \_socialbase_node_get_comment_count()
  2. 8 themes/socialbase/socialbase.theme \_socialbase_node_get_comment_count()
  3. 8.2 themes/socialbase/socialbase.theme \_socialbase_node_get_comment_count()
  4. 8.3 themes/socialbase/socialbase.theme \_socialbase_node_get_comment_count()
  5. 8.4 themes/socialbase/socialbase.theme \_socialbase_node_get_comment_count()
  6. 8.5 themes/socialbase/socialbase.theme \_socialbase_node_get_comment_count()
  7. 8.6 themes/socialbase/socialbase.theme \_socialbase_node_get_comment_count()
  8. 8.7 themes/socialbase/socialbase.theme \_socialbase_node_get_comment_count()

Get comment count for a node.

2 calls to _socialbase_node_get_comment_count()
Field::preprocessElement in themes/socialbase/src/Plugin/Preprocess/Field.php
Preprocess the variables array if an element is present.
Node::preprocessElement in themes/socialbase/src/Plugin/Preprocess/Node.php
Preprocess the variables array if an element is present.

File

themes/socialbase/socialbase.theme, line 91
The primary PHP file for the Social base theme.

Code

function _socialbase_node_get_comment_count(Node $node, $comment_field_name) {
  $comment_count =& drupal_static(__FUNCTION__);

  // Calculate the comment_count for this page request.
  if (is_null($comment_count)) {
    $count = 0;
    $cids = \Drupal::entityQuery('comment')
      ->condition('entity_id', $node
      ->id())
      ->condition('entity_type', 'node')
      ->sort('cid', 'DESC')
      ->execute();
    $comments = Comment::loadMultiple($cids);
    foreach ($comments as $comment) {

      /* @var \Drupal\comment\Entity\Comment $comment */

      // Published main comments or published replies on published main comments
      // are counted for users without 'administer comments' permission.
      if ($comment
        ->isPublished()) {
        if (!$comment
          ->hasParentComment() || $comment
          ->hasParentComment() && $comment
          ->getParentComment()
          ->isPublished()) {
          $count++;
        }
        elseif (\Drupal::currentUser()
          ->hasPermission('administer comments')) {

          // User with 'administer comments' permission can also see the comment
          // as being unpublished, so let's count it.
          $count++;
        }
      }
      elseif (\Drupal::currentUser()
        ->hasPermission('administer comments')) {

        // User with 'administer comments' permission can also see the comment
        // as being unpublished, so let's count it.
        $count++;
      }
    }

    // Make sure our static cache knows it doesnt have to recalculate it this
    // request.
    $comment_count = $count;
  }
  return $comment_count;
}