Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 614
Mir Avatar asked Dec 20 '22 10:12

Mir


2 Answers

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.

like image 54
fietserwin Avatar answered Dec 31 '22 13:12

fietserwin


Assuming that you are using BEF and that you have checked the "Checkboxes/Radio buttons" option for your exposed filter in the BEF options, the way to set check boxes instead of radio buttons is to check "Allow multiple selections" in the configuration dialogue for your exposed filter in Views.

like image 22
alouette Avatar answered Dec 31 '22 15:12

alouette