1

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:
enter image description here
Like:
enter image description here

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

  1. 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. 
}

  1. 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 ?

  • What doesn't work, the discovery? Then check the discovered plugins of this plugin type with 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. – 4k4 Jul 3 at 12:55
  • What doesn't work, in both cases my class doesn't called, and I didn't find the filter in views hi ,I already tested the second option with /plugin/views/filter and it doesn't work, – berramou Jul 3 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.