3 namespace Drupal\history\Plugin\views\field;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\ResultRow;
7 use Drupal\views\ViewExecutable;
8 use Drupal\views\Plugin\views\display\DisplayPluginBase;
9 use Drupal\node\Plugin\views\field\Node;
12 * Field handler to display the marker for new content.
14 * The handler is named history_user, because of compatibility reasons, the
17 * @ingroup views_field_handlers
19 * @ViewsField("history_user_timestamp")
21 class HistoryUserTimestamp extends Node {
26 public function usesGroupBy() {
33 public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
34 parent::init($view, $display, $options);
36 if (\Drupal::currentUser()->isAuthenticated()) {
37 $this->additional_fields['created'] = ['table' => 'node_field_data', 'field' => 'created'];
38 $this->additional_fields['changed'] = ['table' => 'node_field_data', 'field' => 'changed'];
39 if (\Drupal::moduleHandler()->moduleExists('comment') && !empty($this->options['comments'])) {
40 $this->additional_fields['last_comment'] = ['table' => 'comment_entity_statistics', 'field' => 'last_comment_timestamp'];
45 protected function defineOptions() {
46 $options = parent::defineOptions();
48 $options['comments'] = ['default' => FALSE];
53 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
54 parent::buildOptionsForm($form, $form_state);
55 if (\Drupal::moduleHandler()->moduleExists('comment')) {
57 '#type' => 'checkbox',
58 '#title' => $this->t('Check for new comments as well'),
59 '#default_value' => !empty($this->options['comments']),
64 public function query() {
65 // Only add ourselves to the query if logged in.
66 if (\Drupal::currentUser()->isAnonymous()) {
75 public function render(ResultRow $values) {
76 // Let's default to 'read' state.
77 // This code shadows node_mark, but it reads from the db directly and
78 // we already have that info.
80 if (\Drupal::currentUser()->isAuthenticated()) {
81 $last_read = $this->getValue($values);
82 $changed = $this->getValue($values, 'changed');
84 $last_comment = \Drupal::moduleHandler()->moduleExists('comment') && !empty($this->options['comments']) ? $this->getValue($values, 'last_comment') : 0;
86 if (!$last_read && $changed > HISTORY_READ_LIMIT) {
89 elseif ($changed > $last_read && $changed > HISTORY_READ_LIMIT) {
92 elseif ($last_comment > $last_read && $last_comment > HISTORY_READ_LIMIT) {
99 return $this->renderLink(\Drupal::service('renderer')->render($build), $values);