3 namespace Drupal\history\Plugin\views\filter;
5 use Drupal\Core\Cache\UncacheableDependencyTrait;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\views\Plugin\views\filter\FilterPluginBase;
10 * Filter for new content.
12 * The handler is named history_user, because of compatibility reasons, the
15 * @ingroup views_filter_handlers
17 * @ViewsFilter("history_user_timestamp")
19 class HistoryUserTimestamp extends FilterPluginBase {
21 use UncacheableDependencyTrait;
26 public $no_operator = TRUE;
31 public function usesGroupBy() {
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']);
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'];
58 $label = $this->t('Has new content');
61 '#type' => 'checkbox',
63 '#default_value' => $this->value,
71 public function query() {
72 // This can only work if we're authenticated in.
73 if (!\Drupal::currentUser()->isAuthenticated()) {
77 // Don't filter if we're exposed and the checkbox isn't selected.
78 if ((!empty($this->options['exposed'])) && empty($this->value)) {
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;
86 $this->ensureMyTable();
87 $field = "$this->tableAlias.$this->realField";
88 $node = $this->query->ensureTable('node_field_data', $this->relationship);
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";
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");
106 public function adminSummary() {
107 if (!empty($this->options['exposed'])) {
108 return $this->t('exposed');