I have a view with an exposed filters and sorting. They are displayed as a separate block.

I want to hide only sorting widget if view return empty result (filters must left)

How to achieve this?

The problem is that when exposed form is rendered its view isn't executed yet - so i can't determine if view has any result.

  • It's hard to understand some of what you say. Do you want to only display the filter if there are ≥1 options that yield results, or do you want to hide all allowed values that return zero results?
    – beth
    Dec 17, 2012 at 20:19
  • I want hide exposed sorting widget at all if view return empty result. Dec 18, 2012 at 10:32

4 Answers 4


If javascript consider an option, you can hide sorting widget wrapper using javascript on page load when view returns empty result by checking if view result class or id exists in the html

  • I also had in mind this approach. I think it is more user friendly to hide sorting widget by css and show it only if view has results - this will avoid blinking and will works fine with AJAXified views too. Dec 18, 2012 at 10:35

In views-view.tpl.php, just replace this code

<?php if ($exposed): ?>
    <div class="view-filters">
      <?php print $exposed; ?>
  <?php endif; ?>


<?php if ($exposed && $rows): ?>
    <div class="view-filters">
      <?php print $exposed; ?>
  <?php endif; ?>


  • How can I hide only filters which have no filtered results?
    – Dr.Osd
    Oct 6, 2013 at 15:10

This starts to get pretty complicated when you dig into it. Here's the process I've gone through with it:

  1. Using JavaScript: You have a View with an exposed filter. Let's say the options are for whether it's published, and they're "Yes" and "No". Then imagine there is no unpublished content. So the user selects "No" from the exposed filter, and Views returns an empty result. The user can never switch back to the filter value that gives the result, because the filter is now hidden.

  2. Ok, so now you realize what you want is to only hide the filter if all the filter values except the one that's currently being used will return empty results. You can't do this with JavaScript, but that's ok, because there are things like hook_views_pre_render() and hook_form_alter(). You're prepared to write a custom module, so it's all good.

    So you want to look at all the exposed filter options and hide all the ones that would return an empty result. This would mean you would have to perform the query on every available value upon loading the View for the first time. That could be a little bit heavy, but perhaps you build in a boundary, like only try to do this if there are fewer than four available values.

    We'll need to use something like an Entity Field Query to get the results from each available filter value. We do something like this:

    1. Loop through all available filters that we want to limit (e.g. 'status' or 'author' or whatever)
    2. Inside that loop, loop through all the available values of that filter (e.g. 'published' or 'admin' etc.)
    3. Inside each of those, we need to do some kind of query to make sure that filter/value pair returns a result. We'll do an Entity Field Query. You can look at the documented examples, so I'll skip to where we specify what we actually want to look for: -> propertyCondition($value, $filter). We have to do this for every single filter/value pair, but that's not the problem. Because we have to be doing all this in a hook_form_FORM_ID_alter() function in order to be able to hide form elements or allowed value choices, and AFAIK the closest thing I can get to the filter value out of the $form is the displayed choice (e.g. "Yes" for published status, when the Views query actually takes a "1" for "Yes" and "0" for "No".
    4. Ok, so no big, because we can look in $form['view'] to figure that out. So we have our values to pass to our EFQ but we still have a pretty buggy system here, because in a real View our filters work together, whereas here they don't. Plus we haven't built node access into this yet. What if there are unpublished nodes of the type we're talking about but the current user won't get to see them in their View? Oh wait, of the right type? We didn't even include that yet. So basically we have to recreate the whole view every time the view loads to remove the filter values that would return null results.

My verdict: This is too complicated to do reliably with an override.

  • Thanx for detailed answer - as i think - javascript is the only way to do it quickly. Dec 18, 2012 at 10:33

For Drupal 7 views, I added a "Global Text area" in the "No Result behaviour" in advance section of views. In that global text area, i selected text format to be "Full HTML" and added the following javascript

<script type="text/javascript">

  jQuery(function() {
    jQuery('.my-view-class .view-filters').hide("fast");


(change my-view-class with your view's class name)

So this javascript only runs when there are no results and hides the exposed filters and exposed sorts. I think this is the standard way. Hope this helps someone

Muhammad www.app-desk.com

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.