2

I am trying to create a custom exposed filter which would be a select field with years (2015-2025). This would filter the result with the year of the date they are created. I followed this tutorial https://www.flocondetoile.fr/blog/filter-content-year-views-drupal-8 But here there is no solution for ajax submission, so the page does not refresh and the filter does not apply.

Currently I have this year as a contextual filter and the form field in the hook_form_views_exposed_form_alter():

$options = [
    'all' => t('- All -'),
    '2015' => '2015',
    '2016' => '2016',
    '2017' => '2017',
    '2018' => '2018',
    '2019' => '2019',
    '2020' => '2020',
    '2021' => '2021',
    '2022' => '2022',
    '2023' => '2023',
    '2024' => '2024',
    '2025' => '2025',
  ];

  $form['year'] = [
    '#title' => t('By year'),
    '#type' => 'select',
    '#options' => $options,
    '#size' => NULL,
    '#default_value' => 'all',
  ];

Is there a possibility for the filter to work with ajax submission?

1

2 Answers 2

0

You can use this custom filter to filter by year: https://github.com/DarkteK/d8-views-custom-filter/tree/master/d8_filters

Just change the years you want to use in the file 'ViewsYearsFilter.php'

This will work with Ajax

-1

I believe Views Better Exposed Filters can achieve this: What else can I do with BEF?: Provide instant feedback by updating the View using AJAX whenever an exposed filter is changed

Your Answer

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

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