You are here

protected function ViewsYearFilterDatetime::opBetween in Views year filter 8

Override parent method, which deals with dates as integers.

Overrides Date::opBetween

File

src/Plugin/views/filter/ViewsYearFilterDatetime.php, line 68

Class

ViewsYearFilterDatetime
Date/time views filter.

Namespace

Drupal\views_year_filter\Plugin\views\filter

Code

protected function opBetween($field) {

  // If year filter selected.
  if (!empty($this->value['type']) && $this->value['type'] == 'date_year' && isset($this->value['min']) && isset($this->value['max'])) {
    $min = $this->value['min'] ?? 0;
    $max = $this->value['max'] ?? 0;
    $operator = strtoupper($this->operator);

    // In Case of changed, created and published on date is timestamp.
    if (strpos($field, '.changed') !== FALSE || strpos($field, '.created') !== FALSE || strpos($field, '.published_at') !== FALSE) {
      $this->query
        ->addWhereExpression($this->options['group'], "YEAR(FROM_UNIXTIME({$field})) {$operator} {$min} AND {$max}");
    }
    else {
      $this->query
        ->addWhereExpression($this->options['group'], "YEAR({$field}) {$operator} {$min} AND {$max}");
    }
  }
  else {
    parent::opBetween($field);
  }
}