I have 2 exposed filters in my Drupal 8 view - both are select dropdowns. I want the value of the 2nd filter to change based on selection of first filter Here are the filters $filters = $view->display_handler->getOption('filters');

$filters['country'] and $filters['states'].

Problem is that I always get an empty value when I try to access country filter in my views_pre_view hook.

dpm($filters['country']['value']) always prints empty.

How do I solve this please?

1 Answer 1


You don't have access to your filter options inside hook_views_pre_view.

$filters = $view->display_handler->getOption('filters');

give only informations about the filter something like:

 "field_country_target_id" => array:20 [▼
    "id" => "field_country_target_id"
    "table" => "node__field_country"
    "field" => "field_country_target_id"
    "relationship" => "none"
    "group_type" => "group"
    "admin_label" => ""
    "operator" => "or"
    "value" => []
    "group" => 1
    "exposed" => true
    "expose" => array:13 [▶]
    "is_grouped" => false
    "group_info" => array:10 [▶]
    "reduce_duplicates" => false
    "type" => "select"
    "limit" => true
    "vid" => "countries"
    "hierarchy" => false
    "error_message" => true
    "plugin_id" => "taxonomy_index_tid"

To get the selected filter values with:

$filters_selected = $view->getExposedInput(); 

Even you get the filter options you can't alter it because exposed filters are form inputs.
So i thing the best way to achieve this is by using hook_form_FORM_ID_alter like the following.

 * Implements hook_form_FORM_ID_alter().
function MY_MODUOLE_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  // Here is options of your country filter
  // Note that they key is FIELD_NAME_target_id
  // Change the FIELD_NAME with you field name
  $countries = $form['field_country_target_id']['#options'];
  // Get the city options the same way .
   * TODO: To alter options array change $form['field_country_target_id']['#options'] not the $countries
  • Thanks berramou, but how does this help me get the currently value of the countries drop-down filter? I need the value of that filter not just all the options. It's the value of countries filter that will determine values I populate states filter with..
    – ilechcod
    Apr 1, 2020 at 13:39
  • Also $form['field_country_target_id']['#options'] doesn't work. What works for me is $form['country'] ['#options'] and that returns the list of all countries rather than the selected option.
    – ilechcod
    Apr 1, 2020 at 13:41
  • check my edit to get selected values try with $view->getExposedInput()
    – berramou
    Apr 1, 2020 at 13:43
  • i just gave you an example with my field and i add the comment Change the FIELD_NAME with you field name to change it with your field name
    – berramou
    Apr 1, 2020 at 13:45
  • Thanks again berramou for quick reply. I tried the $view->getExposedInput() but got the following output from dpm
    – ilechcod
    Apr 1, 2020 at 14: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.