Programatically change autocomplete views exposed filter by a select list I have a view which has a filter by users that created de nodes. This filter is exposed, in order to select an user and filter the results by this user. I would like to change the default autocomplete filter by a select list of user, and in this list I want to show only some users.
I followed this tutorial but without success. Although I change "uid" form field, It does anything.
How could I do it? Thanks

UPDATE: my code.

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


$pos = stripos($form['#id'], 'views-exposed-form-my-form');
if ($pos !== false) {

    $opcusuarios = array();
    $query = \Drupal::entityQuery('user');
    ... some conditions ...
    $entity_ids = $query-&gt;execute();              
    $usuarios = \Drupal::entityTypeManager()-&gt;getStorage('user')-&gt;loadMultiple($entity_ids);
    foreach($usuarios as $user) {
        $opcusuarios[$user-&gt;id()] = $user-&gt;getAccountName();
    }

    // Change the input
    $form['uid']['#type'] = 'select';
    $form['uid']['#multiple'] = FALSE;

    // Add the $options from above to our select list
    $form['uid']['#options'] = $opcusuarios;
    unset($form['uid']['#size']);
}


}
It won't work because validateExposed function from core/modules/user/src/Plugin/views/filter/Name.php expect the values to be something like :

array:1 [▼
   0 =&gt; array:1 [▼
     "target_id" =&gt; "1"
   ]
 ]


So if you just modified the autocomplete to select you will send the value of uid like 1 for example:

Suggested solution 


Add Validation function before validateExposed and refactor the value of input uid.
Here is code snippet:




/**
 * Implements hook_form_FORM_ID_alter().
 */
function my_module_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  // If not the view we are looking, move on
  if (stripos($form['#id'], 'views-exposed-form-my-form')) {
    return FALSE;
  }
  //- query
  $query = \Drupal::entityQuery('user');
  /**
   * @TODO: Add your conditions.
   */
  $options = [];
  //- get user ids
  $entity_ids = $query->execute();
  //- Get Users List
  $users_list = \Drupal::entityTypeManager()
    ->getStorage('user')
    ->loadMultiple($entity_ids);
 //- Prepare the list options.
  foreach ($users_list as $user) {
    $options[$user->id()] = $user->getAccountName();
  }
  if (isset($form['uid'])) {
    //- alter the Uid field.
    $form['uid'] = [
      '#type'         => 'select',
      '#options'      => $options,
      '#empty_option' => t('-- Select User --'),
      '#multiple'     => FALSE,
    ];
    unset($form['uid']['#size']);
    //- Add Validate in the beginning To be before validateExposed
    array_unshift($form['#validate'], 'validateUserExposedFilter');
  }
}

/** 
 * @param $form
 * @param \Drupal\Core\Form\FormStateInterface $form_state
 */
function validateUserExposedFilter(&$form, FormStateInterface $form_state) {
//- Get the input value
  $input = $form_state->getValue('uid');
  //- Prepare value to be like the expected
  if ($input) {
    $value = [
      0 => [
        "target_id" => $input,
      ],
    ];
    //- Set the new value
    $form['uid']['#value'] = $value;
    //- Update the form state
    $form_state->setValue('uid', $value);
  }
}