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
Comment #1
dawehnerSee http://drupal.org/node/584366#comment-2091430
Comment #2
dubs commentedHi 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: -
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
Comment #3
dawehnerAfaik this methods cannot be used to change the view object when its already build:
so use something like this
Comment #4
dubs commentedHi again,
On the hook_views_pre_view I added this code: -
But still the value doesn't change the output.
Thanks for the new reply though...
David
Comment #5
dawehnerUps
Comment #6
dawehnerMake this active until its solved
Comment #7
merlinofchaos commentedIf 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.
Comment #8
dubs commentedThanks 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
Comment #9
siva_epari commentedI 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.
Comment #10
iamjon commentedDubs,
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.
Comment #11
Alexander Matveev commentedThis doesn't work with AJAX turned on =(
Comment #12
ionmedia commentedhere 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
to change my field_form_serial_value to some value
please, help to do it
Comment #13
bratsun commentedI 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:
Note that I changed display_options array instead of handler.
Tested, works fine for 6.x-2.12.
Comment #14
svnindia commentedThanks guys...