5

I have an exposed filter block on the sidebar on my page. I want to hide the exposed filter block if the result of the view is empty. What is the best was to do it ? Right now i am having some Jquery code in a no result behaviour textfield wich hides some block elements. But i dont like this solution. I prefer if the code is clean and the html code is completely removed.

1

7 Answers 7

8

@pierre's answer is a good one, however $view->total_rows is not always populated.

So you could do something like this instead, which will work no matter what:

function MODULE_views_pre_render(&$view) {
  if ($view->name == 'VIEW_NAME' && $view->current_display == 'DISPLAY_NAME') {
    if (empty($view->result) && empty($view->getExposedInput())) {
      $view->exposed_widgets = NULL;
    }
  }
}
1
  • This solution worked for my use case. Commented Oct 17, 2017 at 22:07
1

In a custom module, implements hook_form_FORM_ID_alter() for the views_exposed_form form. Start by checking if the form is the one from your view and display, then set its #access property FALSE if the views has no results.

function MODULE_form_views_exposed_form_alter (&$form, &$form_state) {
  $view = &$form_state['view'];
  if ($view->name == VIEW_NAME && $view->current_display == DISPLAY_NAME) {
    if (empty($view->total_rows)) {
      $form['#access'] = FALSE;
    }
  }
}
1
  • Most likely $view->name doesn't exist any more, so $view->id() can be used instead.
    – MilanG
    Commented Jun 10, 2020 at 9:48
1

The above suggestions for [function MODULE_form_views_exposed_form_alter(&$form, &$form_state)] are helpful if you want to hide the exposed filters whether or not the results will showing.

So according to yours requirement you can hide filters when no result will found using below code-:

function hook_views_pre_render(&$view){
  if($view->name == VIEW_NAME){
    if(empty($view->result) && isset($view->exposed_widgets)){
        $view->exposed_widgets = FALSE;
      }
   }
}
1

Just one quick correction for Drupal 9 to avoid having the warning:

Warning: Parameter 1 to hook_views_views_pre_render() expected to be a reference, value given.
use Drupal\views\ViewExecutable;
    
/**
 * Implements hook_views_views_pre_render().
 */
function agi_views_views_pre_render(ViewExecutable $view) {
  if ($view->id() === 'view_id'){
    if (empty($view->result) && isset($view->exposed_widgets)) {
      $view->exposed_widgets = FALSE;
    }
  }
}
0

Answer provided by @Juyal Jee is generally correct, but probably outdated. $view->name variable doesn't exist any more. Insted $view->id() method can be used:

function hook_views_pre_render(&$view){
  if($view->id() == VIEW_NAME){
    if(empty($view->result) && isset($view->exposed_widgets)){
      $view->exposed_widgets = FALSE;
    }
  }
}
0

To my surprise, adding a Global: Result summary field in the Header seems to have changed the block behavior.

Before, the block showed on the page with only the Title search box visible. Immediately after I saved the new field, the block stopped showing.

My answer is obviously very unscientific, and I've not tested thoroughly. Are others experiencing the same magic?

-2
  1. Go to the advanced tab of you view
  2. Add contextual filters
  3. Select Global: Null
  4. When the filter value is NOT available: Select 'Hide view'
4
  • Its not working. The View is gone, but the exposed filter block is still there!
    – oll
    Commented Nov 5, 2013 at 12:21
  • Here is a detailed guide: agentic.ca/blog/…
    – Druvision
    Commented Nov 6, 2013 at 8:15
  • The answer is incomplete and misses several steps from the referenced article. Commented Mar 15, 2014 at 19:51
  • For what it's worth, looks like this answer is from drupal.org/forum/support/post-installation/2011-11-16/… and people found it helpful. @astha, are you the author of that comment, too? If so, you have my upvote even though your (useful!) answer does not completely solve the issue for me.
    – Kay V
    Commented May 20, 2022 at 23:05

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.