HistoryUserTimestamp.php 3.11 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\views\ResultRow;
11
use Drupal\views\ViewExecutable;
12
use Drupal\views\Plugin\views\display\DisplayPluginBase;
13
use Drupal\node\Plugin\views\field\Node;
14

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

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

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

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

49 50
  protected function defineOptions() {
    $options = parent::defineOptions();
51 52 53 54 55 56

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

    return $options;
  }

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

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

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

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

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

107
}