HistoryUserTimestamp.php 2.91 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\ViewExecutable;
11
use Drupal\views\Plugin\views\display\DisplayPluginBase;
12
use Drupal\node\Plugin\views\field\Node;
13
use Drupal\Component\Annotation\PluginID;
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
 * @PluginID("history_user_timestamp")
24
 */
25
class HistoryUserTimestamp extends Node {
26

27 28 29 30 31 32
  /**
   * Overrides \Drupal\node\Plugin\views\field\Node::init().
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);

33 34
    global $user;
    if ($user->uid) {
35 36
      $this->additional_fields['created'] = array('table' => 'node_field_data', 'field' => 'created');
      $this->additional_fields['changed'] = array('table' => 'node_field_data', 'field' => 'changed');
37 38 39 40 41 42
      if (module_exists('comment') && !empty($this->options['comments'])) {
        $this->additional_fields['last_comment'] = array('table' => 'node_comment_statistics', 'field' => 'last_comment_timestamp');
      }
    }
  }

43 44
  protected function defineOptions() {
    $options = parent::defineOptions();
45 46 47 48 49 50

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

    return $options;
  }

51 52
  public function buildOptionsForm(&$form, &$form_state) {
    parent::buildOptionsForm($form, $form_state);
53 54 55 56 57 58 59 60 61 62
    if (module_exists('comment')) {
      $form['comments'] = array(
        '#type' => 'checkbox',
        '#title' => t('Check for new comments as well'),
        '#default_value' => !empty($this->options['comments']),
        '#fieldset' => 'more',
      );
    }
  }

63
  public function query() {
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    // Only add ourselves to the query if logged in.
    global $user;
    if (!$user->uid) {
      return;
    }
    parent::query();
  }

  function render($values) {
    // 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;
    global $user;
    if ($user->uid) {
79 80
      $last_read = $this->getValue($values);
      $changed = $this->getValue($values, 'changed');
81

82
      $last_comment = module_exists('comment') && !empty($this->options['comments']) ?  $this->getValue($values, 'last_comment') : 0;
83

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

101
}