HistoryUserTimestamp.php 3.15 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\history\Plugin\views\field\HistoryUserTimestamp.
6 7
 */

8
namespace Drupal\history\Plugin\views\field;
9

10
use Drupal\Core\Form\FormStateInterface;
11
use Drupal\views\ResultRow;
12
use Drupal\views\ViewExecutable;
13
use Drupal\views\Plugin\views\display\DisplayPluginBase;
14
use Drupal\node\Plugin\views\field\Node;
15

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

28 29 30 31 32 33 34
  /**
   * {@inheritdoc}
   */
  public function usesGroupBy() {
    return FALSE;
  }

35 36 37 38 39 40
  /**
   * Overrides \Drupal\node\Plugin\views\field\Node::init().
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);

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

50 51
  protected function defineOptions() {
    $options = parent::defineOptions();
52

53
    $options['comments'] = array('default' => FALSE);
54 55 56 57

    return $options;
  }

58
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
59
    parent::buildOptionsForm($form, $form_state);
60
    if (\Drupal::moduleHandler()->moduleExists('comment')) {
61 62
      $form['comments'] = array(
        '#type' => 'checkbox',
63
        '#title' => $this->t('Check for new comments as well'),
64 65 66 67 68
        '#default_value' => !empty($this->options['comments']),
      );
    }
  }

69
  public function query() {
70
    // Only add ourselves to the query if logged in.
71
    if (\Drupal::currentUser()->isAnonymous()) {
72 73 74 75 76
      return;
    }
    parent::query();
  }

77 78 79 80
  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
81 82 83 84
    // 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;
85
    if (\Drupal::currentUser()->isAuthenticated()) {
86 87
      $last_read = $this->getValue($values);
      $changed = $this->getValue($values, 'changed');
88

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

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

108
}