I want to pass contextual filter arguments programmatically to a view. I know it can be done through views_get_view
and views_embed_view
, but I want to pass it to a view's page display. Also I want to pass multiple arguments to the view, so it won't be good to pass all the arguments in the url.
I tried to pass the arguments using $view->args = implode('+', $args);
in hook_views_pre_render
, but it didn't work.
Does anyone know how to pass contextual filter arguments using any of view hooks?
1 Answer
I got the solution. It can be done using hook_views_pre_view(). Since hook_views_pre_view()
allows altering a view at the very beginning of views processing, before anything is done, I am able to feed the arguments to the view.
function MODULE_NAME_views_pre_view(&$view, &$display_id, &$args) {
if ($view->name == 'myview' && $display_id == 'page_1') {
$tids = custom_function();
$args[] = implode('+', $tids);
}
}
Hope this is useful to someone :)
I want to pass multiple arguments to the view, so it won't be good to pass all the arguments in the url
What kind of arguments do you want to pass? I ask because maybe you can archive it with the Views UI.views_embed_view
or url arguments. While you can write a 'default argument' plugin for views I am not sure how you would feed data to it if not over the url. If you don't want to really pass data but rather generate it somehow, you can use the UI with 'provide default value' and the 'php' option on the contextual filter itself which is the same.hook_views_pre_render
doesn't work as the db was already queried.hook_views_pre_render
, but it can be done usinghook_views_pre_view()
. I got the solution and will update soon.