You are here

function NodeNewComments::pre_render in Views (for Drupal 7) 8.3

Run before any fields are rendered.

This gives the handlers some time to set up before any handler has been rendered.

Parameters

$values: An array of all objects returned from the query.

Overrides FieldPluginBase::pre_render

File

lib/Views/comment/Plugin/views/field/NodeNewComments.php, line 62
Definition of Views\comment\Plugin\views\field\NodeNewComments.

Class

NodeNewComments
Field handler to display the number of new comments.

Namespace

Views\comment\Plugin\views\field

Code

function pre_render(&$values) {
  global $user;
  if (!$user->uid || empty($values)) {
    return;
  }
  $nids = array();
  $ids = array();
  foreach ($values as $id => $result) {
    $nids[] = $result->{$this->aliases['nid']};
    $values[$id]->{$this->field_alias} = 0;

    // Create a reference so we can find this record in the values again.
    if (empty($ids[$result->{$this->aliases['nid']}])) {
      $ids[$result->{$this->aliases['nid']}] = array();
    }
    $ids[$result->{$this->aliases['nid']}][] = $id;
  }
  if ($nids) {
    $query = db_select('node', 'n');
    $query
      ->addField('n', 'nid');
    $query
      ->innerJoin('comment', 'c', 'n.nid = c.nid');
    $query
      ->addExpression('COUNT(c.cid)', 'num_comments');
    $query
      ->leftJoin('history', 'h', 'h.nid = n.nid');
    $query
      ->condition('n.nid', $nids);
    $query
      ->where('c.changed > GREATEST(COALESCE(h.timestamp, :timestamp), :timestamp)', array(
      ':timestamp' => NODE_NEW_LIMIT,
    ));
    $query
      ->condition('c.status', COMMENT_PUBLISHED);
    $query
      ->groupBy('n.nid');
    $result = $query
      ->execute();
    foreach ($result as $node) {
      foreach ($ids[$node->nid] as $id) {
        $values[$id]->{$this->field_alias} = $node->num_comments;
      }
    }
  }
}