I have a page view that has a couple filters which are not exposed to the user. Users are not to have control over them, but they should able to change at render time. I did something like this a year and a half ago but completely forget the process.

Is it possible to execute a new filter at the preprocess stage? I am doing the following below and it seems to be having no effect.

E.g

function MYTHEME_preprocess_views_view(&$vars) {

  if ($vars['name'] == 'MyDisplay') {
     $display_id = $vars['display_id'];  
     switch ($display_id) {
       case 'page_2':
         $view = $vars['view'];
         $filter = $view->get_item($display_id,'filter','field_story_news_type_value_many_to_one');
         $filter['value']['value'] = 2;
         $view->set_item($display_id,'filter','field_story_news_type_value_many_to_one',$filter);
 
        $view->execute_display();

         $vars['view'] = $view;
         break;
     }    
  }

}

Comments

merlinofchaos’s picture

Status: Active » Fixed

At render time the filters are already processed, the query created and executed and results have been rendered.

You can't go back in time.

skalfyfan’s picture

Status: Fixed » Active

Merlin,

Thanks for quick response. Can we modify arguments before rendering then and moving it into a module? I have tried converting my filters to arguments instead and the hooked into pre_render with no results? Any suggestions?

Thanks

function MYMODULE_views_pre_render(&$view) {
  
  if ($view->name == 'CustomPageViews') {
   
    $display_id = $view->current_display;  
     switch ($display_id) {
       case 'page_2':
         // the view takes 2 arguments - trying to force the second argument to the value "5" before rendering
         $view->set_arguments(array('all','5'));
         $view->is_cacheable = FALSE;
         $view->execute();

         // devel seems to still show no argument set for the second argument at this point? :(
         print kpr($view->argument['field_story_news_type_value']);

         break;
     }  

  }
}
skalfyfan’s picture

Resolved.

Same code as above but using the following hook instead resolves the issue.

function MYMODULE_views_pre_view(&$view) {...}
skalfyfan’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

fixed code