I have a view with an entity reference that is used as a filter exposed to the user. When the page is loaded with no arguments, I want to change the default of the filter to be the current user's uid. After some research, I ended up with a solution that works fine if the filter is single select. If it is multi-select, my dpm's indicate that the change was successful, but the query that is executed still reflects the system configured default rather than the override.

Here is the code in question:
/**
* Implements hook_views_pre_build
*
* This is to set the default filter options for the My Pages view to the uid of
* the currently logged in user
*
* For some reason, this works if the filter allows single select, but does not if
* it allows multi-select
*/
function inventory_app_views_pre_build(&$view) {
watchdog("my pages", "In views_post_build");
global $user;
if ($view->name == 'my_pages') {
$query_params = drupal_get_query_parameters();
if (!isset($query_params['field_assignee_target_id'])) {
//$view->display_handler->options['filters']['field_assignee_target_id']['value'] =
// array($user->uid => $user->uid);
$view_filters = $view->display_handler->get_option('filters');
$view_filters['field_assignee_target_id']['value'] =
array($user->uid => $user->uid);
$view->display_handler->override_option('filters', $view_filters);
dpm($view->display_handler, "AFTER OVERRIDE");
}
}
}

Comments

rhuffstedtler created an issue.

pringlz’s picture

Hi) I am seeking for this solution too. For this question is so few articles...

I was creating by this code

$exposed_filters = array(
  'title' => 'My title',   //single value ex.filter
  'term_field_tid' => '1',  //single
// 'term_anotherfield_tid[]' => '20',  //MULTIPLE OR
// 'term_anotherfield_tid[0]' => '20',
);

// Pass in exposed filter values. array_merge() order prioritizes user input.
$view->exposed_input = array_merge($exposed_filters, (array)$view->exposed_input);
$view->exposed_raw_input = array_merge($exposed_filters, (array)$view->exposed_raw_input);
$view->exposed_data = array_merge($exposed_filters, (array)$view->exposed_data);

from here http://www.grasmash.com/article/programmatically-render-drupal-view-expo...

IF "exposed filter field value" is SINGLE -> views execute and filtering with this value.
IF "exposed filter field valueS" is MULTIPLE -> views execute WITHOUT ALL this values.

Code working only with single filter values...

Here another one with this problem http://www.fhhyc.com/setting-exposed-filter-programmatically/

Any solutions? How to override view programmatically with multiple exposed filter's values?

pringlz’s picture

WOW!!!))))

I found solution))

If you want to set multiple values programmatically to view:

$view_filters = $view->display_handler->get_option('filters');
//$view_filters['title']['value'] = 'Title';
$view_filters['field_max_tid']['value'][0] = '1';
$view_filters['field_max_tid']['value'][1] = '2';

$view->display_handler->override_option('filters', $view_filters);

Many thanks to developers of DEVEL module)
For understanding - if filter is single - than value is string, or single array.
if filter is multiple - than value is array, but without ['value'][0] not worked.

Better solutions? You are welcome!
Maybe this is not beatiful code, but all work!)