Hi there,

I am absolutely at my wits end here, having tried for days to get this to work. I've checked the issue queue, documentation (both group and logrus) as well as the advanced help, but cannot find how to achieve this.

I have a view which uses search terms as a filter (they are exposed) and taxonomy terms too. For a couple of reasons (e.g. passing an argument to the search terms filter) I need to alter the value of the filters using PHP code as the view is displayed.

I've been trying to do this via the hooks described here (http://views.doc.logrus.com/group__views__hooks.html). An example of my non-working code is here (I've tried so many approaches - this is just the latest failed attempt!): -

function views_news_filters_views_pre_view(&$view, &$display_id, &$args) {
    if ($view->name == 'properties') { 
        $item = $view->get_item($display_id, 'filter', 'keys');
        $item['value'] = "london"; // just an example
        $view->set_item($view->current_display, 'filter', 'keys', $item);
        $view->is_cachable = FALSE;
    }
}

Sadly, this doesn't work, probably for several reasons. For one, the exposed value is not visible at this point.

Later in the views hook, I've kprint_r'd the view and can see the value is still there, but the results look no different.

I've tried using later hooks and then executing the displays, but this just causes recursions and white screens.

Which hook would be appropriate to alter the value, and how do you actually make the display use the new filter values? Can a short example please be provided, or please point me to another article if this has already been covered.

Thanks,

David

Comments

dawehner’s picture

Status: Active » Fixed
dubs’s picture

Hi Dereine,

Thanks for a quick reply and that link. I've already read this comment in my searching but still couldn't see how to implement the filter change.

Merlin says: -

Either use hook_views_pre_view() or instantiate the handler after adding it and add directly to $view->filter[] array. The latter may actually be kind of difficult because handler instantiations are cached so the hook_views_pre_view() option may be better.

I've used the hooks_views_pre_view approach, as you can see in my sample code. The filter already exists, so I don't want to add another one, I just want to edit the existing one but can't seem to make the value actually do anything to the output.

So, I still wanted to know how to actually get the value to take.

Thanks,

David

dawehner’s picture

Afaik this methods cannot be used to change the view object when its already build:

so use something like this

$view->display_handler->handlers['filter']['keys']['value'] = 'test';
dubs’s picture

Hi again,

On the hook_views_pre_view I added this code: -

$view->display_handler->options['handlers']['filters']['keys']['value'] = 'london';

But still the value doesn't change the output.

Thanks for the new reply though...

David

dawehner’s picture

Ups

$view->display_handler->options['handlers']['filters']['keys']->value = 'london';
dawehner’s picture

Status: Fixed » Active

Make this active until its solved

merlinofchaos’s picture

If the handler object is already instantiated, modifying the value in the options won't do you any good. If $view->filter['id_of_filter'] is already instantiated, set the value on that object instead.

You should avoid modifying display_options directly; that's a recipe for causing pain.

dubs’s picture

Thanks for getting back to be Merlin. The $view->filter array is not available on the pre_view, so I'll try a few combinations of this on each of the available hooks and post feedback later.

Thanks again,

David

siva_epari’s picture

I used the following function to change the vocab id set in the taxonomy filter in the views ui

For 'default' display

function MyModule_views_pre_view(&$view, &$display_id, &$args) {
if($view->name == 'MyView'){
$view->display['default']->handler->options['filters']['tid']['vid'] = 2;
}
}

For multiple displays

function MyModule_views_pre_view(&$view, &$display_id, &$args) {
if ($view->name == 'MyView') {
foreach ($view->display as $view_display) {
if ($view_display->handler->options['filters']['tid']['vid']) {
$view_display->handler->options['filters']['tid']['vid'] = 4;
} elseif ($view_display->handler->handlers['filter']['tid']->options['vid']) {
$view_display->handler->handlers['filter']['tid']->options['vid'] = 4;
}
}
}
}

And the view is working perfectly.

iamjon’s picture

Status: Active » Closed (fixed)

Dubs,
Were you ever able to solve this? If yes, please update so others can benefit.
I'm assuming that epari.siva's suggestion worked.
marking this as fixed.

Alexander Matveev’s picture

This doesn't work with AJAX turned on =(

ionmedia’s picture

here my filter array from dsm($views)

filter (Array, 6 elements)
type (Object) views_handler_filter_node_type
∞ (Recursion)
gid (Object) domain_views_handler_filter_domain_access_gid
∞ (Recursion)
uid (Object) views_handler_filter_user_name
∞ (Recursion)
date_filter (Object) date_api_filter_handler
∞ (Recursion)
status (Object) views_handler_filter_boolean_operator
∞ (Recursion)
field_form_serial_value (Object) content_handler_filter_numeric
∞ (Recursion)

i can't understand what i must change in this query

$view->display_handler->options['handlers']['filters']['keys']->value = 'london';

to change my field_form_serial_value to some value

please, help to do it

bratsun’s picture

I was trying to filter webform submissions by node id (there's no argument yet) and came across this thread. I ended up with the following solution as none of the above worked out:

$view = views_get_view('Replies');
$view->display['default']->display_options['filters']['ad_id']['value'] = $node->nid; 
print $view->execute_display('default'); 

Note that I changed display_options array instead of handler.

Tested, works fine for 6.x-2.12.

svnindia’s picture

Thanks guys...