HistoryUserTimestamp.php 3.03 KB
Newer Older
1 2
<?php

3
namespace Drupal\history\Plugin\views\field;
4

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;
10

11 12 13
/**
 * Field handler to display the marker for new content.
 *
14 15
 * The handler is named history_user, because of compatibility reasons, the
 * table is history.
16 17
 *
 * @ingroup views_field_handlers
18
 *
19
 * @ViewsField("history_user_timestamp")
20
 */
21
class HistoryUserTimestamp extends Node {
22

23 24 25 26 27 28 29
  /**
   * {@inheritdoc}
   */
  public function usesGroupBy() {
    return FALSE;
  }

30
  /**
31
   * {@inheritdoc}
32 33 34 35
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);

36
    if (\Drupal::currentUser()->isAuthenticated()) {
37 38
      $this->additional_fields['created'] = array('table' => 'node_field_data', 'field' => 'created');
      $this->additional_fields['changed'] = array('table' => 'node_field_data', 'field' => 'changed');
39
      if (\Drupal::moduleHandler()->moduleExists('comment') && !empty($this->options['comments'])) {
40
        $this->additional_fields['last_comment'] = array('table' => 'comment_entity_statistics', 'field' => 'last_comment_timestamp');
41 42 43 44
      }
    }
  }

45 46
  protected function defineOptions() {
    $options = parent::defineOptions();
47

48
    $options['comments'] = array('default' => FALSE);
49 50 51 52

    return $options;
  }

53
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
54
    parent::buildOptionsForm($form, $form_state);
55
    if (\Drupal::moduleHandler()->moduleExists('comment')) {
56 57
      $form['comments'] = array(
        '#type' => 'checkbox',
58
        '#title' => $this->t('Check for new comments as well'),
59 60 61 62 63
        '#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()) {
67 68 69 70 71
      return;
    }
    parent::query();
  }

72 73 74 75
  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
76 77 78 79
    // Let's default to 'read' state.
    // This code shadows node_mark, but it reads from the db directly and
    // we already have that info.
    $mark = MARK_READ;
80
    if (\Drupal::currentUser()->isAuthenticated()) {
81 82
      $last_read = $this->getValue($values);
      $changed = $this->getValue($values, 'changed');
83

84
      $last_comment = \Drupal::moduleHandler()->moduleExists('comment') && !empty($this->options['comments']) ? $this->getValue($values, 'last_comment') : 0;
85

86
      if (!$last_read && $changed > HISTORY_READ_LIMIT) {
87 88
        $mark = MARK_NEW;
      }
89
      elseif ($changed > $last_read && $changed > HISTORY_READ_LIMIT) {
90 91
        $mark = MARK_UPDATED;
      }
92
      elseif ($last_comment > $last_read && $last_comment > HISTORY_READ_LIMIT) {
93 94
        $mark = MARK_UPDATED;
      }
95 96
      $build = array(
        '#theme' => 'mark',
97
        '#status' => $mark,
98
      );
99
      return $this->renderLink(drupal_render($build), $values);
100 101
    }
  }
102

103
}