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 ?

3
  • 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.
    – 4uk4
    Jul 3, 2019 at 12:55
  • see drupal.stackexchange.com/questions/252874/…
    – 4uk4
    Jul 3, 2019 at 13:06
  • 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, 2019 at 13:06

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.