When using Drupal 7 and Views 3, what is the correct way to alter a Views filter value so that it affects the content that's just about to be rendered?
I tried everything mentioned here for D6.
Even though a few people got it to work, Merlin states that modifying the display_options is the incorrect way to achieve this, but he's vague in his response as to what to do (http://drupal.org/node/789710#comment-2927556).
I have tried:
function pages_views_pre_view($view){
$view = views_get_view('north_carolina');
$view->set_display('default');
$view->display_handler->options['filters']['province']['value'] = 'Georgia';
dsm($view->display_handler->options['filters']['province']);
}
Notes:
1) I have a view created called north_carolina
that has two types of displays - a page and a block (page_1 and block_1).
2) my filters are the same across my displays so I am trying to change the 'default' display so that it affects all of them.
3) The default value of my filter is 'North Carolina' (which I set in the Views UI) but notice I am trying to change it to 'Georgia'
4) If I run DSM immediately after, I see that the filter has been changed, however, my results still show 'North Carolina' results. Caching is turned off, and I still get the same thing after running drush cc all.
I have also tried running that same code through hook_views_pre_build
and hook_views_pre_execute
.
Any suggestions?
EDIT:
As suggested it may help to have additional information. Here is a dump of the view:
$view = new view();
$view->name = 'north_carolina';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'North Carolina';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'test nc block';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['label'] = '';
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
/* Sort criterion: Content: Post date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'node';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Location: Province */
$handler->display->display_options['filters']['province']['id'] = 'province';
$handler->display->display_options['filters']['province']['table'] = 'location';
$handler->display->display_options['filters']['province']['field'] = 'province';
$handler->display->display_options['filters']['province']['value'] = 'North Carolina';
$handler->display->display_options['filters']['province']['exposed'] = TRUE;
$handler->display->display_options['filters']['province']['expose']['operator_id'] = 'province_op';
$handler->display->display_options['filters']['province']['expose']['label'] = 'State';
$handler->display->display_options['filters']['province']['expose']['operator'] = 'province_op';
$handler->display->display_options['filters']['province']['expose']['identifier'] = 'province';
$handler->display->display_options['filters']['province']['expose']['remember_roles'] = array(
2 => '2',
1 => 0,
3 => 0,
4 => 0,
);
/* Display: Block */
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->display->display_options['block_description'] = 'test nc block';
Note that these handlers are provided by the locations module. Specifically for the "Location: Province" filter I am given an auto complete form. When I type in North Carolina, it pops up, and I have to choose that. However, notice in the SQL statement that views generates it uses the two character abbreviation for state:
SELECT node.title AS node_title, node.nid AS nid, node.created AS node_created
FROM
{node} node
LEFT JOIN {location_instance} location_instance ON node.vid = location_instance.vid
LEFT JOIN {location} location ON location_instance.lid = location.lid
WHERE (( (node.status = '1') AND (location.province = 'NC') ))
ORDER BY node_created DESC
LIMIT 10 OFFSET 0