1 views_handler_field_node_new_comments.inc views_handler_field_node_new_comments::pre_render(&$values)

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 views_handler_field::pre_render

File

core/modules/comment/views/views_handler_field_node_new_comments.inc, line 56
Definition of views_handler_field_node_new_comments.

Class

views_handler_field_node_new_comments
Field handler to display the number of new comments.

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) {
    $result = db_query("SELECT n.nid, COUNT(c.cid) as num_comments FROM {node} n INNER JOIN {comment} c ON n.nid = c.nid
        LEFT JOIN {history} h ON h.nid = n.nid AND h.uid = :h_uid WHERE n.nid IN (:nids)
        AND c.changed > GREATEST(COALESCE(h.timestamp, :timestamp), :timestamp) AND c.status = :status GROUP BY n.nid  ", array(
      ':status' => COMMENT_PUBLISHED,
      ':h_uid' => $user->uid,
      ':nids' => $nids,
      ':timestamp' => NODE_NEW_LIMIT,
    ));

    foreach ($result as $node) {
      foreach ($ids[$node->nid] as $id) {
        $values[$id]->{$this->field_alias} = $node->num_comments;
      }
    }
  }
}