1. Home
  2. Code snippet

Alter views exposed form

Created on Jan 14, 2023
/**
 * Implements hook_form_FORM_ID_alter().
 */
function MODULE_NAME_form_views_exposed_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  $view = $form_state->getStorage();
  $view_id = $view['view']->id();
  $display_id = $view['view']->current_display;
  if ($view_id === 'THE_VIEW_ID' &&  $display_id === 'THE_DISPLAY_ID_eg:page_1') {
    $form['foo'] = ['YOUR_FILTER'];
  }
}