I have an existing view in which I want to change the available options in a view exposed filter. I can't manipulate the form because the modifications are dependant on other views data, so I thought I could use a view hook:

function my_mdoule_views_pre_view($view, $display_id, $args) {
  // This doesn't work unfortunately
  if ($display_id === 'gallery_media_select_modal') {

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

    $filters['bundle']['operator'] = 'IN';
    $filters['bundle']['value'] = ['image' => 'image'];

    $view->display_handler->overrideOption('filters', $filters);


This setting is basically the same that you can do in the UI, but like I said I need to do it dynamically.

I can see that this is in fact set correctly (and still set in later view hooks) but it's never picked up by the code that is responsible for building the exposed select.

I've found many similar questions, but conclusively they've only been answered for Drupal 7, e.g. https://groups.drupal.org/node/82219

  • How about a form alter hook? The Views exposed filter is a form and can be altered through form alter hooks.
    – mvdgun
    Sep 1, 2017 at 7:14
  • Technically the easiest way, but this hook doesn't have the view context, so I cannot remove options based on the view config. But I realized that my assumption about the other hook in my question is actually wrong anyway, so I think the only way to solve this is a custom view filter plugin.
    – Narretz
    Sep 5, 2017 at 12:33

2 Answers 2


I think you are very nearly on the right track, and I don't think you necessarily need a hook. I was able to perform essentially the same task with the following code in a class method.

// Get reference to your view.
$storage = \Drupal::entityTypeManager()->getStorage('view');
$view = $storage->load($view_id);

// Be sure to get a specific display.
$display = $view->getExecutable()->getDisplay();

// Retrieve filters. If the combine fields filter is present,
// edit it then assign back using `overrideOption`.
$options = $display->getOption('filters');
if (isset($options['combine'])) {
  $options['combine']['fields'][$field_name] = $field_name;
  $display->overrideOption('filters', $options);

// This is the critical missing step.

I hope this helps.

  • This worked for programmatically enabling/disabling a view's menu link.
    – codesmith
    Mar 30, 2021 at 19:30

I have used hook_views_query_alter() and that works.

$condition is an array with the keys field, value and operator.

use Drupal\views\ViewExecutable; 
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\node\Entity\Node;

function wb_oh_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  foreach ($query->where as &$condition_group) {
    foreach ($condition_group['conditions'] as &$condition) {

      if (strpos($condition['field'], 'node__field_bank_tenure')) {
        $condition['field'] = str_replace('2005-01-01', '2015-01-01', $condition['field']);
        $condition['field'] = str_replace('2007-12-30', '2017-12-30', $condition['field']);

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.