Drupal 8 - how to get exposed filters value in preprocess_views_view

8.9k Views Asked by At

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!

2

There are 2 best solutions below

1
Hubert On BEST ANSWER

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 }}
0
shamalainen On

I had problems with @Hubert's solution and managed to get it to work with:

$variables["searchInputValue"] = $view->exposed_raw_input['query'];