Setting exposed filter default value from query string I have a view which show products and an exposed filter block (category, manufacturer, price).

I want to set category default value from URL, like this:

/browse-products/category-id

though, I wrote this module which take the parameter from URL and put it into exposed filter default value.

function mymodule_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  global $request;

  if ($form_id == 'views_exposed_form') {
    $category_id = (int)($request-&gt;attributes-&gt;get('_raw_variables')-&gt;get('arg_0'));
    $form['category_id']['#default_value'][$category_id] = $category_id;
    dpm($form['category_id']);
  }
}


dpm output for /browse-products/5

Array
    (
        [#type] =&gt; checkboxes
        [#multiple] =&gt; 1
        [#options] =&gt; Array
            (
                [5] =&gt; Tablets
                [10] =&gt; -Ainovo
                [7] =&gt; -Galaxy Tab
                [6] =&gt; -IPad
                [1] =&gt; Smart Phones
                [4] =&gt; -Galaxy
                [3] =&gt; -IPhone
                [9] =&gt; --IPhone C
                [8] =&gt; --IPhone X
            )

        [#size] =&gt; 9
        [#default_value] =&gt; Array
            (
                [5] =&gt; 5
            )

        [#bef_nested] =&gt; 1
        [#theme] =&gt; bef_checkboxes
    )


but it doesn't work?

another thing, why dpm($form) prints the views exposed form data twice!

update 1: I tried something like this (but didn't work):

function mymodule_views_pre_build(Drupal\views\ViewExecutable $view) {
    $view_filters = $view-&gt;display_handler-&gt;getOption('filters');
    $view_filters['field_category_target_id']['value'][8] = 8;
    $view-&gt;display_handler-&gt;setOption('filters', $view_filters);
}


or this:

function mymodule_views_pre_build(Drupal\views\ViewExecutable $view) {
  $filters = $view-&gt;getExposedInput();
  $filters['field_category_target_id'] = 8;
  $view-&gt;setExposedInput($filters);
}
The #default_value parameter should be set to the key of the select list array.

Example:

$form['category_id']['#default_value'] = $category_id;