1

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. – Gun5m0k3 Sep 1 '17 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 '17 at 12:33
0

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.
$view->getExecutable()->setDisplay($display_id);
$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.
$view->save();

I hope this helps.

0

I used to query after and it's worked.

$condition is an array include (field, value, 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, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.