You are here

function _socialbase_node_get_comment_count in Open Social 8.9

Same name and namespace in other branches
  1. 8 themes/socialbase/socialbase.theme \_socialbase_node_get_comment_count()
  2. 8.2 themes/socialbase/socialbase.theme \_socialbase_node_get_comment_count()
  3. 8.3 themes/socialbase/socialbase.theme \_socialbase_node_get_comment_count()
  4. 8.4 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.

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 90
The primary PHP file for the Social base theme.

Code

function _socialbase_node_get_comment_count(NodeInterface $node) {
  $comment_count =& drupal_static(__FUNCTION__ . $node
    ->id());

  // Calculate the comment_count for this page request.
  if (is_null($comment_count)) {
    $count = 0;
    $comment_storage = \Drupal::entityTypeManager()
      ->getStorage('comment');
    $current_user = \Drupal::currentUser();
    $cids = $comment_storage
      ->getQuery()
      ->condition('entity_id', $node
      ->id())
      ->condition('entity_type', 'node')
      ->sort('cid', 'DESC')
      ->execute();
    $pid_query = $comment_field_data_query = \Drupal::database()
      ->select('comment_field_data', 'cfd');
    foreach ($cids as $cid) {

      // Published main comments or published replies on published main comments
      // are counted for users without 'administer comments' permission.
      $query = $comment_storage
        ->getQuery()
        ->condition('cid', $cid)
        ->condition('status', TRUE);

      // Check if comment is published.
      if (!empty($query
        ->execute())) {
        $comment_field_data_query
          ->addField('cfd', 'pid');
        $comment_field_data_query
          ->condition('cid', $cid);
        $pid = $comment_field_data_query
          ->execute()
          ->fetchField();

        // Check if comment has parent comment.
        if ($pid === '0' || $pid_query
          ->condition('cid', $pid)
          ->condition('status', TRUE)) {
          $count++;
        }
        elseif ($current_user
          ->hasPermission('administer comments')) {

          // User with 'administer comments' permission can also see the comment
          // as being unpublished, so let's count it.
          $count++;
        }
      }
      elseif ($current_user
        ->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;
}