How to prevent views from loading the results upfront

by Mathankumar   Last Updated September 11, 2019 21:07 PM

I have views block with exposed filters and ajax enabled which was rendered in home page, the problem is when the block is loaded with exposed form views is loading the results too. However I want the results needs to be shown only when the user clicks on the submit button.

If I enable input required, this will work, but the client doesn't want this.

Tags : 7 views blocks


Answers 5


  1. Edit your view and click on advanced to reveal more options
  2. in the Exposed form block click on Exposed form style - basic
  3. click on the option "input required"

You're done.

saadlulu
saadlulu
October 09, 2012 13:43 PM

Here is the way to solve your issue:

  1. Add Contextual filter of "Global: Null" type
  2. In "When the filter value is NOT available" section check "Provide default value" and specify "Fixed value" and "all". Notice! If view still display results - then try select "Raw value from URL" here.
  3. In "When the filter value IS available or a default is provided" section check "Specify validation criteria" and select "PHP code"
  4. Specify this code

    if (count($view->exposed_input)) { return TRUE; }

  5. In "Action to take if filter value does not validate" select "Display all results for the specified field" value
  6. Don't forget to add any "No results behavior"!

Views filter configuration

Now results will be shown ONLY if filters are sent. View will be empty on first display.

Eugene Fidelin
Eugene Fidelin
October 10, 2012 10:04 AM

  $view = views_get_view("VIEW NAME");
  $view->set_display('DISPLAY ID');
  $view->init_handlers();
  $form_state = array(
    'view' => $view,
    'display' => $view->display_handler->display,
    'exposed_form_plugin' => $view->display_handler->get_plugin('exposed_form'),
    'method' => 'get',
    'rerender' => TRUE,
    'no_redirect' => TRUE,
  );
  $form = drupal_build_form('views_exposed_form', $form_state);
  return drupal_render($form);

Just add the above code in a block.

Sandesh Manghale
Sandesh Manghale
March 19, 2014 08:06 AM

Add an exposed filter and hide it( using css etc). When somebody clicks "Apply" then change the hidden filter value using Javascript and submit.

Initially set the exposed filter value to not to generate any View results

AgA
AgA
March 19, 2014 08:28 AM

muchas Gracias! Sos un genio!!!

ayelen
ayelen
September 11, 2019 20:03 PM

Related Questions



Drupal 8 views - Message contextual filtering

Updated July 11, 2018 20:07 PM


In the snatch face a 177 dealers smart

Updated June 13, 2015 08:03 AM

Website consuming more memory

Updated August 26, 2016 08:04 AM