drupal-viewsdrupal-8drupal-exposed-filter

Drupal 8 - how to get exposed filters value in preprocess_views_view


I'm using preprocess_views_view to define some new variables and pass them to a twig template.

In order to define these variables I need to access the exposed filters input values, but I can't seem to figure out how:

function my_modules_preprocess_views_view(&$variables) {
  $view = $variables['view'];
  // Here I would need to access the exposed filters value
  $exposed_filter_value = "the_value";
  $variables["foo"] = "Something based on the exposed filters value";
}

I would be very grateful for any clues - cheers!


Solution

  • In a hook_preprocess_views_view() implementation in your theme or module:

    $values = $view->getExposedInput();
    // for example $values["color"];
    

    Or, you can access the values directly from the views-view.html.twig template:

    // Assuming `color` is configured to be the Filter identifier in the 
    // view's Filter Criteria exposed filter.
    {{ view.getExposedInput.color }}