ViewsYearFilterDate.php in Views year filter 8        
                          
                  
                        
  
  
  
  
  
File
  src/Plugin/views/filter/ViewsYearFilterDate.php
  
    View source  
  <?php
namespace Drupal\views_year_filter\Plugin\views\filter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\Date;
use Drupal\views_year_filter\DateViewsTrait;
class ViewsYearFilterDate extends Date {
  use DateViewsTrait;
  
  protected function valueForm(&$form, FormStateInterface $form_state) {
    parent::valueForm($form, $form_state);
    if (!$form_state
      ->get('exposed')) {
      $form['value']['type']['#options']['date_year'] = $this
        ->t('A date in CCYY format.');
      
      $form['#attached']['library'][] = 'views_year_filter/year_filter';
    }
  }
  
  protected function opSimple($field) {
    
    if (!empty($this->value['type']) && $this->value['type'] == 'date_year' && isset($this->value['value']) && filter_var($this->value['value'], FILTER_VALIDATE_INT)) {
      
      $value = $this->value['value'] ?? '';
      
      if (strpos($field, '.changed') !== FALSE || strpos($field, '.created') !== FALSE || strpos($field, '.published_at') !== FALSE) {
        $this->query
          ->addWhereExpression($this->options['group'], "YEAR(FROM_UNIXTIME({$field})) {$this->operator} {$value}");
      }
      else {
        
        $this->query
          ->addWhereExpression($this->options['group'], "YEAR({$field}) {$this->operator} {$value}");
      }
    }
    else {
      parent::opSimple($field);
    }
  }
  
  protected function opBetween($field) {
    
    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);
      
      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);
    }
  }
  
  public function buildExposedForm(&$form, FormStateInterface $form_state) {
    parent::buildExposedForm($form, $form_state);
    $this
      ->applyDatePopupToForm($form);
  }
}