Alter views exposed form drupal 8 I have a view that in that view I set up 3 exposed filter, all working fine.

I need to set a condition to show the third filter option to a specific user, it can be checked according to the user role, but how I should do that?

Can anyone provide a hook to help me?

Previously I tried the following but it does not change anything:

function my_modulename_form_alter(&amp;$form, $form_state, $form_id) {

  if ($form_id == 'views_exposed_form') {

    //unset($form['average']);

    var_dump($form);

    die();
  }
}


My view looks like this
Following example unsets the filter average when user is NOT administrator, you can modify it to your specific case.

use Drupal\Core\Form\FormStateInterface;
function my_modulename_form_alter(&amp;$form, FormStateInterface $form_state, $form_id){

  $roles = \Drupal::currentUser()-&gt;getRoles();
  $is_user_admin = in_array("administrator", $roles);
  $is_exposed = $form_id == 'views_exposed_form';
  $is_filter_set = isset($form['average']);

  if ( $is_exposed &amp;&amp; $is_filter_set &amp;&amp; !$is_user_admin ) {
    unset($form['average']);
  }
}


If you want to unset only on a specific view exposed form use this condition

$is_our_view = $form["#id"] == "views-exposed-form-article-page-1";