Hello.

I have a view with a filter. I ned to catch up a value from argument, convert it and pass it to view filter.
I hav created the following code.

function customcode_views_pre_view(&$view, &$display_id, &$args){
  $view->display['default']->handler->options['filters']['field_raion_value']['value']  = array(
  'val1' => 'val1'
  ); 
}

The code is worked well - I have tested it with debug, but it has no effect. The filter value doesnt changed.
Drupal 7 and views 3.
Can you help me?

Comments

Stefan Lehmann’s picture

Have a look here. He's fetching a value from the $_SESSION array, but it should be easy enough to adapt to your use case.

PS: btw .. you probably want to filter the View by at least it's ID .. otherwise you will apply the filter to every view which you most certainly don't want.

I like cookies!

ricobanga’s picture

I would try :

$filters = $view->display_handler->get_option('filters');
// change your filter here
$view->display_handler->override_option('filters', $filters);
lykyd’s picture

That helped me, I just want to add that in drupal 8 it will be :

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

Anatolii1309’s picture

$filters = $view->display_handler->getOption('filters');
if ($filters['field_filter']) {
  foreach ($filters['field_filter']['value'] as $key => $filter) {
      unset($filters['field_filter']['value'][$key]);
  }
}
$view->display_handler->overrideOption('filters', $filters);

For hook_views_pre_view(&$view)

cirrus3d’s picture

This works great on a standard Views page, but it does not seem to work on a Views pane in Panels. Any suggestions?

Shyghar’s picture

Same as him.
Not working in panels.

rapindrive’s picture

@ricobanga

works :D

balapalanisamy’s picture

You can try hook_views_default_views_alter in your custom module to change the filters of a particular view.

https://api.drupal.org/api/views/views.api.php/function/hook_views_defau...

Ex:

function YOUR_MODULE_views_default_views_alter(&$views) {
  if (isset($views['VIEW_NAME'])) {
    //dpm($views['VIEW_NAME']->display['DISPLAY_NAME']->display_options['filters']);
    $views['VIEW_NAME']->display['DISPLAY_NAME']->display_options['filters']; // you will get an array of the fields in the filter
  }
}