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 ?
drupal plugin:debug views.filter
? At first glance the fully-qualified class name in the second example looks odd, should contain\Plugin\views
. Also if you introduce a new plugin like in the first example you have to connect it in views data.