2

I can call a view with contextual filters without any problem, but when I add exposed filter value $view->setExposedInput to the call, I just does not work, the exposed value is not applied and being ignored. The filter 'price' field is set in the views filters options and i tried both exposing it and not exposing it to visitors without any luck. I call this view from my controller class and I need to pass the arguments to both filters and I do not need exposed form to be displayed to the user.

$view = Views::getView($view_id);
if (is_object($view)) {
  $filters = [
       'price' => 500, //my exposed filter value       
     ];      
  $view->setExposedInput($filters);
  $view->setArguments($args);
  $view->setDisplay($display_id);
  $view->preExecute();
  $view->execute();
  $view->buildRenderable($display_id, $args);
  $content = $view->render();
}
3
  • I do not need exposed form to be displayed to the user ... you just contradicted your question's title
    – No Sssweat
    Commented Dec 27, 2018 at 2:37
  • Well, this is not so important for now.. the thing I want to do is to render a view results programmatically with passing contextual and exposed filters data.
    – JetSet
    Commented Dec 27, 2018 at 17:12
  • setting Use Ajax: True may be relevant here Commented Oct 13, 2020 at 6:33

1 Answer 1

1

You can use this code in custom block can also set permission.

 use Drupal\Core\Form\FormState; 
 use Drupal\views\Views; 
   $form = []; 
   $view_id = 'blog_landing_page'; 
   $display_id = 'page_1'; 
   $view = Views::getView($view_id);     
   if ($view) { 
   $view->setDisplay($display_id); 
   $view->initHandlers(); 
 $form_state = (new FormState()) 
  ->setStorage([ 
   'view' => $view, 
   'display' => &$view->display_handler->display, 
   'rerender' => TRUE, 
   ]) 
   ->setMethod('get') 
   ->setAlwaysProcess() 
   ->disableRedirect(); 
   $form_state->set('rerender', NULL); 
 $form = \Drupal::formBuilder()->buildForm('\Drupal\views\Form\ViewsExposedForm', $form_state); 
    } 

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.