0

I don't know how to make my custom exposed filter work with AJAX:

I have a view that has a contextual filter that filters for Node creation date by year. This filter gets filled via a query parameter like '?year=2021'.

Via the code below I have created a custom exposed filter form element to allow the user to select a year.

This works great unless AJAX is activated. How can I make this work with AJAX enabled?

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymodule_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (isset($form['#id']) && $form['#id'] == 'views-exposed-form-news-page-1') {

    $years = ['all' => t('- All -')];
    for ($i = date('Y'); $i >= '1997'; $i--) {
      $years[$i] = $i;
    }

    $form['year'] = [
      '#title' => new TranslatableMarkup('Year'),
      '#type' => 'select',
      '#options' => $years,
      '#size' => NULL,
      '#default_value' => 'all',
    ];

  }
}
0

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.