function NodeNewComments::pre_render

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

drupal/core/modules/comment/lib/Drupal/comment/Plugin/views/field/NodeNewComments.php, line 60
Definition of Drupal\comment\Plugin\views\field\NodeNewComments.

Class

NodeNewComments
Field handler to display the number of new comments.

Namespace

Drupal\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' => HISTORY_READ_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;
      }
    }
  }
}