1

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?

4
  • 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.
    – No Sssweat
    Commented Jul 20, 2016 at 10:32
  • I want to pass multiple tids, I get those tids from custom solr search, so there might be a huge number of tids.
    – Suresh R
    Commented Jul 20, 2016 at 10:33
  • I don't think this can be done without either 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_renderdoesn't work as the db was already queried.
    – Zuzuesque
    Commented Jul 20, 2016 at 12:27
  • Yes, you are partially right @Zuzuesque. It can't be done with hook_views_pre_render, but it can be done using hook_views_pre_view(). I got the solution and will update soon.
    – Suresh R
    Commented Jul 20, 2016 at 13:13

1 Answer 1

8

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 :)

Your Answer

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

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