View source
<?php
namespace Drupal\social_event\Plugin\views\filter;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\filter\InOperator;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;
class EventDate extends InOperator {
protected $valueFormType = 'radios';
const UPCOMING_EVENTS = 1;
const PAST_EVENTS = 2;
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
$this->definition['options callback'] = [
$this,
'generateOptions',
];
}
public function query() {
$value = (int) current($this->value);
if (empty($value)) {
return;
}
$this
->ensureMyTable();
$now = $this->query
->getDateFormat('NOW()', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, TRUE);
$configuration = [
'table' => 'node__field_event_date_end',
'field' => 'entity_id',
'left_table' => '',
'left_field' => 'nid',
];
$join = Views::pluginManager('join')
->createInstance('standard', $configuration);
$alias = $this->query
->addRelationship($configuration['table'], $join, 'node_field_data');
$field_end = $this->query
->getDateFormat($alias . '.field_event_date_end_value', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, TRUE);
$field = "{$this->tableAlias}.{$this->realField}";
$field = $this->query
->getDateFormat($field, DateTimeItemInterface::DATETIME_STORAGE_FORMAT, TRUE);
switch ($value) {
case self::UPCOMING_EVENTS:
$this->query
->addWhereExpression($this->options['group'], "({$field} >= {$now}) OR ({$field} <= {$now} AND {$field_end} > {$now})");
break;
case self::PAST_EVENTS:
$this->query
->addWhereExpression($this->options['group'], "\n ({$now} >= {$field_end})\n OR\n ({$field_end} IS NULL AND {$now} >= {$field})");
break;
}
}
public function generateOptions() {
return [
self::UPCOMING_EVENTS => $this
->t('Ongoing and upcoming events'),
self::PAST_EVENTS => $this
->t('Past events'),
];
}
}