617cd23e12d530ab68fe57ff381950ef1c1b13fa
[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   /**
46    * {@inheritdoc}
47    */
48   protected function defineOptions() {
49     $options = parent::defineOptions();
50
51     $options['comments'] = ['default' => FALSE];
52
53     return $options;
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
60     parent::buildOptionsForm($form, $form_state);
61     if (\Drupal::moduleHandler()->moduleExists('comment')) {
62       $form['comments'] = [
63         '#type' => 'checkbox',
64         '#title' => $this->t('Check for new comments as well'),
65         '#default_value' => !empty($this->options['comments']),
66       ];
67     }
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function query() {
74     // Only add ourselves to the query if logged in.
75     if (\Drupal::currentUser()->isAnonymous()) {
76       return;
77     }
78     parent::query();
79   }
80
81   /**
82    * {@inheritdoc}
83    */
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.
88     $mark = MARK_READ;
89     if (\Drupal::currentUser()->isAuthenticated()) {
90       $last_read = $this->getValue($values);
91       $changed = $this->getValue($values, 'changed');
92
93       $last_comment = \Drupal::moduleHandler()->moduleExists('comment') && !empty($this->options['comments']) ? $this->getValue($values, 'last_comment') : 0;
94
95       if (!$last_read && $changed > HISTORY_READ_LIMIT) {
96         $mark = MARK_NEW;
97       }
98       elseif ($changed > $last_read && $changed > HISTORY_READ_LIMIT) {
99         $mark = MARK_UPDATED;
100       }
101       elseif ($last_comment > $last_read && $last_comment > HISTORY_READ_LIMIT) {
102         $mark = MARK_UPDATED;
103       }
104       $build = [
105         '#theme' => 'mark',
106         '#status' => $mark,
107       ];
108       return $this->renderLink(\Drupal::service('renderer')->render($build), $values);
109     }
110   }
111
112 }