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'];
48 protected function defineOptions() {
49 $options = parent::defineOptions();
51 $options['comments'] = ['default' => FALSE];
59 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
60 parent::buildOptionsForm($form, $form_state);
61 if (\Drupal::moduleHandler()->moduleExists('comment')) {
63 '#type' => 'checkbox',
64 '#title' => $this->t('Check for new comments as well'),
65 '#default_value' => !empty($this->options['comments']),
73 public function query() {
74 // Only add ourselves to the query if logged in.
75 if (\Drupal::currentUser()->isAnonymous()) {
84 public function render(ResultRow $values) {
85 // Let's default to 'read' state.
86 // This code shadows node_mark, but it reads from the db directly and
87 // we already have that info.
89 if (\Drupal::currentUser()->isAuthenticated()) {
90 $last_read = $this->getValue($values);
91 $changed = $this->getValue($values, 'changed');
93 $last_comment = \Drupal::moduleHandler()->moduleExists('comment') && !empty($this->options['comments']) ? $this->getValue($values, 'last_comment') : 0;
95 if (!$last_read && $changed > HISTORY_READ_LIMIT) {
98 elseif ($changed > $last_read && $changed > HISTORY_READ_LIMIT) {
101 elseif ($last_comment > $last_read && $last_comment > HISTORY_READ_LIMIT) {
102 $mark = MARK_UPDATED;
108 return $this->renderLink(\Drupal::service('renderer')->render($build), $values);