You are here

function _socialbase_node_get_comment_count in Open Social 8.4

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.5 themes/socialbase/socialbase.theme \_socialbase_node_get_comment_count()
  6. 8.6 themes/socialbase/socialbase.theme \_socialbase_node_get_comment_count()
  7. 8.7 themes/socialbase/socialbase.theme \_socialbase_node_get_comment_count()
  8. 8.8 themes/socialbase/socialbase.theme \_socialbase_node_get_comment_count()

Get comment count for a node.

1 call to _socialbase_node_get_comment_count()
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 79
The primary PHP file for the Social base theme.

Code

function _socialbase_node_get_comment_count(Node $node, $comment_field_name) {
  $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 */
    if ($comment
      ->isPublished()) {

      // Published main comments or published replies on published main comments
      // are counted for users without 'administer comments' permission.
      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++;
    }
  }
  return $count;
}