f33dab25aa52521a7d581a998bc2402ad15db096
[yaffs-website] / web / core / modules / history / src / Plugin / views / field / HistoryUserTimestamp.php
1 <?php
2
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  * Field handler to display the marker for new content.
13  *
14  * The handler is named history_user, because of compatibility reasons, the
15  * table is history.
16  *
17  * @ingroup views_field_handlers
18  *
19  * @ViewsField("history_user_timestamp")
20  */
21 class HistoryUserTimestamp extends Node {
22
23   /**
24    * {@inheritdoc}
25    */
26   public function usesGroupBy() {
27     return FALSE;
28   }
29
30   /**
31    * {@inheritdoc}
32    */
33   public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
34     parent::init($view, $display, $options);
35
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'];
41       }
42     }
43   }
44
45   protected function defineOptions() {
46     $options = parent::defineOptions();
47
48     $options['comments'] = ['default' => FALSE];
49
50     return $options;
51   }
52
53   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
54     parent::buildOptionsForm($form, $form_state);
55     if (\Drupal::moduleHandler()->moduleExists('comment')) {
56       $form['comments'] = [
57         '#type' => 'checkbox',
58         '#title' => $this->t('Check for new comments as well'),
59         '#default_value' => !empty($this->options['comments']),
60       ];
61     }
62   }
63
64   public function query() {
65     // Only add ourselves to the query if logged in.
66     if (\Drupal::currentUser()->isAnonymous()) {
67       return;
68     }
69     parent::query();
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function render(ResultRow $values) {
76     // Let's default to 'read' state.
77     // This code shadows node_mark, but it reads from the db directly and
78     // we already have that info.
79     $mark = MARK_READ;
80     if (\Drupal::currentUser()->isAuthenticated()) {
81       $last_read = $this->getValue($values);
82       $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         $mark = MARK_NEW;
88       }
89       elseif ($changed > $last_read && $changed > HISTORY_READ_LIMIT) {
90         $mark = MARK_UPDATED;
91       }
92       elseif ($last_comment > $last_read && $last_comment > HISTORY_READ_LIMIT) {
93         $mark = MARK_UPDATED;
94       }
95       $build = [
96         '#theme' => 'mark',
97         '#status' => $mark,
98       ];
99       return $this->renderLink(drupal_render($build), $values);
100     }
101   }
102
103 }