Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / history / src / Plugin / views / filter / HistoryUserTimestamp.php
1 <?php
2
3 namespace Drupal\history\Plugin\views\filter;
4
5 use Drupal\Core\Cache\UncacheableDependencyTrait;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\views\Plugin\views\filter\FilterPluginBase;
8
9 /**
10  * Filter for new content.
11  *
12  * The handler is named history_user, because of compatibility reasons, the
13  * table is history.
14  *
15  * @ingroup views_filter_handlers
16  *
17  * @ViewsFilter("history_user_timestamp")
18  */
19 class HistoryUserTimestamp extends FilterPluginBase {
20
21   use UncacheableDependencyTrait;
22
23   /**
24    * {@inheritdoc}
25    */
26   public $no_operator = TRUE;
27
28   /**
29    * {@inheritdoc}
30    */
31   public function usesGroupBy() {
32     return FALSE;
33   }
34
35   /**
36    * {@inheritdoc}
37    */
38   public function buildExposeForm(&$form, FormStateInterface $form_state) {
39     parent::buildExposeForm($form, $form_state);
40     // @todo There are better ways of excluding required and multiple (object flags)
41     unset($form['expose']['required']);
42     unset($form['expose']['multiple']);
43     unset($form['expose']['remember']);
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   protected function valueForm(&$form, FormStateInterface $form_state) {
50     // Only present a checkbox for the exposed filter itself. There's no way
51     // to tell the difference between not checked and the default value, so
52     // specifying the default value via the views UI is meaningless.
53     if ($form_state->get('exposed')) {
54       if (isset($this->options['expose']['label'])) {
55         $label = $this->options['expose']['label'];
56       }
57       else {
58         $label = $this->t('Has new content');
59       }
60       $form['value'] = [
61         '#type' => 'checkbox',
62         '#title' => $label,
63         '#default_value' => $this->value,
64       ];
65     }
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function query() {
72     // This can only work if we're authenticated in.
73     if (!\Drupal::currentUser()->isAuthenticated()) {
74       return;
75     }
76
77     // Don't filter if we're exposed and the checkbox isn't selected.
78     if ((!empty($this->options['exposed'])) && empty($this->value)) {
79       return;
80     }
81
82     // Hey, Drupal kills old history, so nodes that haven't been updated
83     // since HISTORY_READ_LIMIT are bzzzzzzzt outta here!
84     $limit = REQUEST_TIME - HISTORY_READ_LIMIT;
85
86     $this->ensureMyTable();
87     $field = "$this->tableAlias.$this->realField";
88     $node = $this->query->ensureTable('node_field_data', $this->relationship);
89
90     $clause = '';
91     $clause2 = '';
92     if ($alias = $this->query->ensureTable('comment_entity_statistics', $this->relationship)) {
93       $clause = "OR $alias.last_comment_timestamp > (***CURRENT_TIME*** - $limit)";
94       $clause2 = "OR $field < $alias.last_comment_timestamp";
95     }
96
97     // NULL means a history record doesn't exist. That's clearly new content.
98     // Unless it's very very old content. Everything in the query is already
99     // type safe cause none of it is coming from outside here.
100     $this->query->addWhereExpression($this->options['group'], "($field IS NULL AND ($node.changed > (***CURRENT_TIME*** - $limit) $clause)) OR $field < $node.changed $clause2");
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function adminSummary() {
107     if (!empty($this->options['exposed'])) {
108       return $this->t('exposed');
109     }
110   }
111
112 }