15

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
0

2 Answers 2

8

It can be tough to figure out the exact part of the views object to modify. I usually export the view first and look at the generated code to get me started.

The snippet below is unmodified and working as expected on one of my production sites. Hopefully it's enough to get you headed in the right direction (obviously, this is in a custom module named offer_select). If you can provide the code from the exported view, someone might be able to take a look.

//Alter the End date filter on the offer views
function offer_select_views_pre_view(&$view) {
  if ($view->name == 'active_offers') {
    $view->display['default']->handler->options['filters']['field_end_value']['value']['value'] = time();
  }
}

--UPDATE--
For your specific view I installed the Location module and created a few nodes in NC and a few in Georgia and imported your view to test. In my custom module (named dev) I started with this code (with devel installed):

function dev_views_pre_view(&$view) {
  if ($view->name == 'north_carolina') {
    dpm($view->display['default']->handler->options['filters']);
  }
}

From there, I continued to add the obvious values displayed in krumo on to the array until I got here:

function dev_views_pre_view(&$view) {
  if ($view->name == 'north_carolina') {
    dpm($view->display['default']->handler->options['filters']['province']['value']);
  }
}

Which simply printed North Carolina in Krumo. At that point I changed from printing the value to setting it like this:

function dev_views_pre_view(&$view) {
  if ($view->name == 'north_carolina') {
    $view->display['default']->handler->options['filters']['province']['value'] = 'Georgia';
  }
}

And voila. That did the trick.

3
  • Cool. When I have a chance, I'l play around with your view and see if I can come up with a more concrete answer. Mar 26, 2013 at 1:57
  • @blue928 see above. I have updated my answer with an answer specific to your questions and general instructions on how I got there. Mar 26, 2013 at 15:19
  • that worked great. Note about the correct answer: Part of my problem is that I did not need to call views_get_view or set the display.
    – blue928
    Mar 27, 2013 at 4:55
2

In case someone has this thread popped up while searching for same solution on D8 (as I did):

New API allows to manipulate these parameters in a lot more clear way. See this section for references.

Small example

function dev_views_pre_view(ViewExecutable &$view, $display_id, array &$args) {
  if ($display_id === 'my_unique_display_name') {
      $args[0] = 'desired_value';
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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