Distinct options in a views exposed filter: The Views Selective Filters Module

Distinct options in a views exposed filter: The Views Selective Filters Module

Difficulty: 
Piece of Cake

You have built an application where there was a taxonomy or options field with more values defined in them than what was really being used after release. And these fields are being used as exposed filters in a View. This basically means that you end up with an option in an exposed filter that yields no results when selected. Not a good UI behaviour, and confusing for the end user. This is where the Views Selective Filters module comes in handy: it will limit the exposed filter options to only those that are present in the result set.

There are many possible use cases for the Views Selective Filters module, and in this post I will walk you through some basic usage.

First of all you need to install and enable the module. Unluckily for you, there is a BUG in Views that needs to be fixed before this module will work (Issue #2159347). The patch has already made it to Drupal 8 (thanks to david_garcia and claudiu_cristea for their effort, go buy them a beer!) but the D7 backport still needs to be commited.

Now we will be creating a view in which we will display a list of all the content in the site, and an exposed filter to filter by content type.

Go to /admin/structure/views and select "Create a new View":

Now give a name to your view and select "Continue and Edit":

Change the default display type to Table, and add a Content Type field that is excluded from display:

Go to the Add Filter option. After enabling the Views Selective Filters, for every filter in the system you will get an additional one with the "(selective)" suffix. Look for the "Content Type (selective)" filter:

Now you must configure the filter:

  • Display Field (1): This is the field (previously added) that will be used to populate the exposed filter options. This field can be hidden or visible, and even rewritten. Only fields that are compatible with the selected filter will be shown here.
  • Sort (2): Pick ascending or descending alphabetical sort. When using an Options field, you will see an option to sort using the original Options definition order.
  • Limit (3): If you unproperly configure your view and the field used to populate the field's content returns too many results, you can crash the view. To prevent that a default of 100 limit is set for the exposed options. For example, if you try following the steps in this post using the Node ID field and filter, and you have 2,000 nodes in your site, then you will get 2,000 elements in the exposed filter (good luck trying not to get an out of memory error...).

Save the filter and you are ready to go! You now have an exposed filter that will only show the content types that are being used. If one of your content types has no content, it will not appear in the exposed filter.

Final Words

This is a very simple - and lightweight - yet helpful module that you probably will need on any website if you are using views that expose taxonomy or options fields.

There is only one caveat with this module: the number of available filters effectively doubles. Other modules such as transilteration (i18n) also have a similar effect, so that for a field you will get different filter versions (original, translated, format, selective). But this is only an issue on sites with a very large number of fields, and well, it just means a very long (and slow) list of filters when adding a new filter to a view - nothing to really worry about.

Comments

I want to have selective filter for Date.The procedure i followed are:
1. Add a field "Date" in view fields with Date format 'Y' (shows only year)
2. selected 'Date(selective)' filter in filter option
The problem is it doesn't show unique values, means if there are 3 contents in the year 2015 , it shows 2015 3 times. I want it to display only once. Please help me to overcome this problem.

There is an issue for that here as a feature request: https://www.drupal.org/node/2215585 You are welcome to fix it yourself, pay someone to fix it, or wait until some fixes it. Seeing the level of activity for the issue, the third will probably take decades.

Hi,
How Can I change the text of the button "apply" to "aplicar" like this in your view?
Thanks

Add new comment

By: david_garcia Sunday, February 1, 2015 - 00:00