Mir
Mir

Reputation: 91

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?

Upvotes: 4

Views: 12404

Answers (4)

Varshith
Varshith

Reputation: 574

I think no answer here answers the question. "What I want is to display a single checkbox that when checked applies/enables the filter, and when unchecked ignores/disables the filter. I don't think Better Exposed filters allows me to do that."

You dont need "Better Exposed filters" to do this. You need to use grouped filters.

  • Click expose this filter to visitors
  • Filter type to expose: Grouped filters
  • Check 'Optional'
  • Check 'Widget type: Radios'
  • Check 'Allow multiple selections'
  • Remove/empty 'Label' if needed
  • In the bottom you will have few rows where you can set each one as you wish. If you want only one, remove all but one
  • Click Apply

This answer is very late. But I hope this helps someone.

Upvotes: 2

fietserwin
fietserwin

Reputation: 217

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.

Upvotes: 5

alouette
alouette

Reputation: 63

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.

Upvotes: 3

NickWebman
NickWebman

Reputation: 188

You would assume that Views would do this out of the box. It doesn't. Use the Better Exposed Filters module -> http://drupal.org/project/better_exposed_filters

It even has nested checkboxes/radio buttons for taxonomy with hierarchy.

Here's some documentation specific to what you're trying to do...

"Checkboxes/Radio buttons: This option is available for any filter that has a limited number of options. Tick the Force single option to use radio buttons, untick it for checkboxes.

Nested Checkboxes/Radio Buttons: While this option shows for any filter that the regular checkboxes option shows for, it's really only of use for taxonomy filters with hierarchy. If you're using a taxomomy filter with Selection type set to Dropdown and Show hierarchy in dropdown ticked, these filters will be rendered as nested, unordered lists. Tick the Force single option to use nested radio buttons"

Upvotes: 1

Related Questions