You are here

public function EventDate::query in Open Social 10.1.x

Same name in this branch
  1. 10.1.x modules/social_features/social_event/src/Plugin/views/filter/EventDate.php \Drupal\social_event\Plugin\views\filter\EventDate::query()
  2. 10.1.x modules/social_features/social_event/src/Plugin/views/sort/EventDate.php \Drupal\social_event\Plugin\views\sort\EventDate::query()
Same name and namespace in other branches
  1. 8.9 modules/social_features/social_event/src/Plugin/views/filter/EventDate.php \Drupal\social_event\Plugin\views\filter\EventDate::query()
  2. 10.3.x modules/social_features/social_event/src/Plugin/views/filter/EventDate.php \Drupal\social_event\Plugin\views\filter\EventDate::query()
  3. 10.0.x modules/social_features/social_event/src/Plugin/views/filter/EventDate.php \Drupal\social_event\Plugin\views\filter\EventDate::query()
  4. 10.2.x modules/social_features/social_event/src/Plugin/views/filter/EventDate.php \Drupal\social_event\Plugin\views\filter\EventDate::query()

Add this filter to the query.

Due to the nature of fapi, the value and the operator have an unintended level of indirection. You will find them in $this->operator and $this->value respectively.

Overrides InOperator::query

File

modules/social_features/social_event/src/Plugin/views/filter/EventDate.php, line 46

Class

EventDate
Filter events by start date and end date.

Namespace

Drupal\social_event\Plugin\views\filter

Code

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;
  }
}