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;
23 // Don't display empty space where the operator would be.
24 public $no_operator = TRUE;
29 public function usesGroupBy() {
33 public function buildExposeForm(&$form, FormStateInterface $form_state) {
34 parent::buildExposeForm($form, $form_state);
35 // @todo There are better ways of excluding required and multiple (object flags)
36 unset($form['expose']['required']);
37 unset($form['expose']['multiple']);
38 unset($form['expose']['remember']);
41 protected function valueForm(&$form, FormStateInterface $form_state) {
42 // Only present a checkbox for the exposed filter itself. There's no way
43 // to tell the difference between not checked and the default value, so
44 // specifying the default value via the views UI is meaningless.
45 if ($form_state->get('exposed')) {
46 if (isset($this->options['expose']['label'])) {
47 $label = $this->options['expose']['label'];
50 $label = $this->t('Has new content');
53 '#type' => 'checkbox',
55 '#default_value' => $this->value,
60 public function query() {
61 // This can only work if we're authenticated in.
62 if (!\Drupal::currentUser()->isAuthenticated()) {
66 // Don't filter if we're exposed and the checkbox isn't selected.
67 if ((!empty($this->options['exposed'])) && empty($this->value)) {
71 // Hey, Drupal kills old history, so nodes that haven't been updated
72 // since HISTORY_READ_LIMIT are bzzzzzzzt outta here!
74 $limit = REQUEST_TIME - HISTORY_READ_LIMIT;
76 $this->ensureMyTable();
77 $field = "$this->tableAlias.$this->realField";
78 $node = $this->query->ensureTable('node_field_data', $this->relationship);
82 if ($ces = $this->query->ensureTable('comment_entity_statistics', $this->relationship)) {
83 $clause = ("OR $ces.last_comment_timestamp > (***CURRENT_TIME*** - $limit)");
84 $clause2 = "OR $field < $ces.last_comment_timestamp";
87 // NULL means a history record doesn't exist. That's clearly new content.
88 // Unless it's very very old content. Everything in the query is already
89 // type safe cause none of it is coming from outside here.
90 $this->query->addWhereExpression($this->options['group'], "($field IS NULL AND ($node.changed > (***CURRENT_TIME*** - $limit) $clause)) OR $field < $node.changed $clause2");
93 public function adminSummary() {
94 if (!empty($this->options['exposed'])) {
95 return $this->t('exposed');