NodeNewComments.php 3.65 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\comment\Plugin\views\field\NodeNewComments.
6 7
 */

8
namespace Drupal\comment\Plugin\views\field;
dawehner's avatar
dawehner committed
9

10
use Drupal\views\Plugin\views\field\Numeric;
11
use Drupal\views\ViewExecutable;
dawehner's avatar
dawehner committed
12
use Drupal\Core\Annotation\Plugin;
13 14 15 16 17

/**
 * Field handler to display the number of new comments.
 *
 * @ingroup views_field_handlers
18
 *
dawehner's avatar
dawehner committed
19
 * @Plugin(
20 21
 *   id = "node_new_comments",
 *   module = "comment"
dawehner's avatar
dawehner committed
22 23 24
 * )
 */
class NodeNewComments extends Numeric {
25

26
  /**
27
   * Overrides Drupal\views\Plugin\views\field\FieldPluginBase::init().
28
   */
29 30
  public function init(ViewExecutable $view, &$options) {
    parent::init($view, $options);
31

32 33 34 35 36
    $this->additional_fields['nid'] = 'nid';
    $this->additional_fields['type'] = 'type';
    $this->additional_fields['comment_count'] = array('table' => 'node_comment_statistics', 'field' => 'comment_count');
  }

37 38
  protected function defineOptions() {
    $options = parent::defineOptions();
39 40 41 42 43 44

    $options['link_to_comment'] = array('default' => TRUE, 'bool' => TRUE);

    return $options;
  }

45
  public function buildOptionsForm(&$form, &$form_state) {
46 47 48 49 50 51 52
    $form['link_to_comment'] = array(
      '#title' => t('Link this field to new comments'),
      '#description' => t("Enable to override this field's links."),
      '#type' => 'checkbox',
      '#default_value' => $this->options['link_to_comment'],
    );

53
    parent::buildOptionsForm($form, $form_state);
54 55
  }

56
  public function query() {
57
    $this->ensureMyTable();
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    $this->add_additional_fields();
    $this->field_alias = $this->table . '_' . $this->field;
  }

  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) {
81 82 83 84 85 86 87 88 89 90
      $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();
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
      foreach ($result as $node) {
        foreach ($ids[$node->nid] as $id) {
          $values[$id]->{$this->field_alias} = $node->num_comments;
        }
      }
    }
  }

  function render_link($data, $values) {
    if (!empty($this->options['link_to_comment']) && $data !== NULL && $data !== '') {
      $node = entity_create('node', array(
        'nid' => $this->get_value($values, 'nid'),
        'type' => $this->get_value($values, 'type'),
      ));
      $this->options['alter']['make_link'] = TRUE;
      $this->options['alter']['path'] = 'node/' . $node->nid;
      $this->options['alter']['query'] = comment_new_page_count($this->get_value($values, 'comment_count'), $this->get_value($values), $node);
      $this->options['alter']['fragment'] = 'new';
    }

    return $data;
  }

  function render($values) {
    $value = $this->get_value($values);
    if (!empty($value)) {
      return $this->render_link(parent::render($values), $values);
    }
    else {
      $this->options['alter']['make_link'] = FALSE;
    }
  }
123

124
}