I seen many answers for Drupal 7, but I can't find any information for Drupal 9.

In hook_form_alter(), $form['field_assigned_location_target_id']['#default_value'] = '553'; doesn't alter the default value for an exposed filter, which uses the value in the settings.

$form['field_assigned_location_target_id']['#value'] = '553'; changes the value initially displayed on the filter, but it's the value submitted from the exposed filter form, independently from the value the users want to submit.

  • 1
    What kind of field is the exposed filter? Code is slightly different depending on what kind of field it is. Commented Dec 4, 2020 at 15:39
  • I tried it for textfield as well as for select. The results are the same. Commented Dec 4, 2020 at 21:40
  • I think you are using the wrong hook. This hook will alter the form, but not the view, which I think will have already been processed. A views hook is probably what you're looking for. I would start with hook_views_pre_execute() or hook_views_pre_render(), or use a preprocess hook and alter the View executable object. api.drupal.org/api/drupal/…
    – Beau
    Commented Dec 7, 2020 at 15:40
  • @PatrickKenny, can you elucidate your comment and/or point to something that explains what and how they are different? I am trying to change the default value of a select filter. Commented Aug 25, 2022 at 20:39

1 Answer 1


I know the question is old, but hopefully this will help someone else. After working extensively to use hook_views_pre_view, I finally found the solution by switching to hook_views_pre_build. This code allowed me to make the changes I needed.

 * Implements hook_views_pre_build().
function  msul_newsletter_helper_views_pre_build(Drupal\views\ViewExecutable $view){
   * to check the format of the argument for setExposedInput, view the results
   * of getExposedInput on your view WHICH HAS HAD EXPOSED FILTERS SUBMITTED.
   * I use dpm($view->getExposedInput());

  //always check the view ID so you don't affect all views.
    //check display id if you don't want to affect all displays in view.
    if( $view->current_display == 'page_2') {
        //in my case I get $editionList from a database query.

        // because I am putting this as a default, I don't worry about other input,
        // but if you are concerned about other input, modify the array
        // from $view->getExposedInput() instead of just placing the information. 

    //in this case, I affect 2 displays.
    if($view->current_display == 'page_4'
        || $view->current_display == 'block_1'
        //in my case I get $latestEdition from a database query.
  • But once this code works, you won't be able to use other filters. Commented Feb 23 at 9:00

Your Answer

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

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