How do I override the date filter to filter by years instead of exact date? I want to add another option to Date filter to filter with only years not the exacte date.
So i want to add another radio option here:

Like:


The above options added by Date Filter.
I have tried two ways:


Add new filter like date filter:
modules/custom/my_module/src/Plugin/views/filter/DateYear.php




namespace Drupal\my_module\Plugin\views\filter;

use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\NumericFilter;

/**
 * Filter to handle dates stored as a timestamp.
 *
 * @ingroup views_filter_handlers
 *
 * @ViewsFilter("date_year")
 */
class DateYear extends NumericFilter {
  // Same Code Date class with a little changes. 
}




I tried to extend Date class and alter date filter plugin. 


I create Class DateYear extending Date filter class:





namespace Drupal\my_module\Plugin\views\filter;

use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\Date;
/**
 * Filter to handle dates stored as a timestamp.
 *
 * @ingroup views_filter_handlers
 *
 * @ViewsFilter("date_year")
 */
class DateYear extends Date {
  // Override valueForm method..
}




Then i implement hook_views_plugins_filter_alter to override class.




   /**
 * Implements hook_views_plugins_filter_alter().
 */
function cam_views_views_plugins_filter_alter(array &$plugins) {
  if ($plugins['date']) {
    $plugins['date']['class'] = 'Drupal\my_module\Plugin\views\filter\DateYear';
  }
}



But none of the above works ?
I contribute a module for this check it out here:


  This module add views filter by year exact, instead of full date like mm-dd-yyyy you can use date year like yyyy.  


https://www.drupal.org/project/views_year_filter