1

I have a block view, and I want to change my query, to add contextual filter. I made a views query alter but when the view appears in sidebar as block view this function doesn't work.

function bpm_views_query_alter(&$view, &$query) {
   if ($view->name == 'single_job' && $view->display_handler->display->id =='block_1' ){
      $nid = _get_job_nid_from_url();
      $query->where[1]['conditions'][2]['value'] = $nid;
   }
}

If I add the same block view in page content then views query alter work as expected. The page is created by page callback and this is following code how i add block view in page content

$output = views_embed_view('single_job', $display_id = 'block_1', $category);
  • in views_embed_view, shouldn't $category be $nid? – keva Jun 28 '12 at 14:23
  • yes it is $nid, but I copy just a piece of my code. I don't have a problem with $output, i just added that to show how everything works perfect with views_post_render. But views post render prints $output in page['content'] not in sidebar. I need to print view in sidebar and I want to assign $nid as contextual filter. – miske Jun 29 '12 at 0:44
1

I think you problem is there that $nid not passed correctly,edit your views, instead of your filter, set filter by contextual filter then in your page passed it by contextual filter parameter with

$view= views_embed_view('single_job', $display_id = 'block_1', $category);
$view->set_arguments(array($node->nid));
$output = $view->preview();
print $output;

not longer need query alter function

/*
function bpm_views_query_alter(&$view, &$query) {
  if ($view->name == 'single_job' && $view->display_handler->display->id =='block_1' ){
    $nid = _get_job_nid_from_url();
  $query->where[1]['conditions'][2]['value'] = $nid; 
 }
}*/
0

You can get the values from the url with $view->args[0] or $_GET['q'].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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