Expose a Drupal Views filter as an on/off checkbox to enable/disable the filter

Mir picture Mir · Feb 6, 2013 · Viewed 10.1k times · Source

I have a Drupal view that filters on a taxonomy field. I would like to be able to toggle the filter on/off by exposing a checkbox on the Exposed Filters form. To illustrate the problem, say for example the Vocabulary is Fruit, and the Terms are Apples, Pears, Oranges and Lemons. I can select citrus fruit content by creating a filter that says Fruit is one of (Oranges, Lemons). Now I would like to expose the filter to allow me to choose whether or not to apply my citrus fruits filter. How can I add a checkbox to the exposed filters forms that will apply the filter when selected, and ignore the filter when no selected?

Answer

fietserwin picture fietserwin · Sep 26, 2014

This is possible:

  • Add a filter on the appropriate field (term reference to Fruits in your example).

In the Configure filter criterion dialog:

  • Set 'Filter type to expose' to 'Grouped filters'.
  • Check "Allow multiple selections"
  • Set 'Widget Type' to Radios.
  • You probably want to empty 'Label'.
  • Remove all but one of the filter sets in the table below.

In that remaining filter set:

  • Fill in the label ('Citrus fruits' in your example).
  • Set 'Operator ' to 'Is one of'.
  • Under Value select the values you want to filter on (Oranges and Lemons in your example).

This gives you 1 checkbox, labeled 'Citrus Fruits'. So the UI is there. Unfortunately, issue [#2224601] prevents that it works, as it results in an invalid query, but a working patch is available.