2

I am using Better Exposed Filters Module and Has Boyfriend field is Boolean Yes/No field type of the content.

The Current View I have is: Toggle filter as Checkbox

When I click the Yes Checkbox I get: Toggle filter as Checkbox on

My quest is when the checkbox is not-checked it'd only display girls without boyfriend like: (please ignore the filters here, I just showed it to explain my question, the filter should not have No, it should be default filter) Toggle filter views checked

I can acheive this with such multiple-checkbox as above but I have the requirements to display only Girls without Boyfriend as default display list. When the checkbox Yes is clicked it'd hide all the Girls without boyfriends and display only Girls who has boyfriend.

There were few similar questions like this when I was searching like This and This but the answers doesn't satisfy my requirements.

4 Answers 4

6

Not totally sure if this is the same as your requirements, but I managed to have one checkbox that only shows relevant content when checked. Basically, I chose these options in filter options:

  1. Expose this filter to visitors, to allow them to change it
  2. Filter type to expose: Single filter
  3. Is one of: (select the 'on' value)
  4. Allow multiple selections (checked)
  5. Limit list to selected items (checked)

Then in BEF: 6. Display exposed filter as: Checkboxes/radio buttons.

So, pretty much the only difference is the selection of "Limit list to selected items" in the filter options. See pic below. (And it doesn't use jquery, so should be more performant) Hope that helps.

.

BEF filter options

0
3

Whenever I run into issues like these, I tend to use the grouped option with exposed filters. You shouldnt need better exposed filters at all for this.

  1. Choose 'Grouped Filter' options
  2. Choose 'radios'
  3. uncheck 'optional' to make the filter required (and not display the -any- option)
  4. Choose 'allow multiple selections' so one can choose either/and and so that checkboxes appear.
  5. Configure the grouped filters similarly (I used the panopoly 'featured' boolean field since it was readily available).

Grouped Filter Settings

2
  • I tried the exact thing after posting the question, this will just eliminate the use of BEF, but there appears Multiple checkbox like featured,non featured (from your example) with BEF. My quest is to display only featured as default and non-featured when checkbox is checked. It'd display again featured when the checkbox is unchecked.
    – user12947
    Commented Nov 4, 2013 at 8:15
  • Ah I see. In this specific case you'll need to follow the example using jquery below or use a form_alter() Commented Nov 18, 2013 at 20:39
2

I fixed this with Simple jQuery functions.

At first I set the views filter to default as No. Like:

enter image description here

Then I hid the div that contains No checkbox with CSS Class element-invisible like:

$('div.containsNoCheckbox').addClass('element-invisible');

Then I toggled between checkbox On/Off with:

$('#YesId').change(function () {                 
                 if($('#NoId').is(':checked')) {
                     $('#NoId').prop('checked', false );
                 }
            });

When I check the Yes Checkbox, this will Uncheck the No Checkbox.

When I Uncheck the Yes Checkbox, default value defined in the filter will be used. This how it toggles now.

1
  • Before accepting my own answer, I am waiting for the answers which can do this in Drupal way, so, I can award the bounty.
    – user12947
    Commented Nov 7, 2013 at 10:44
1

I wanted to implement a similar approach and I finally found a solution, perhaps it will help some others.

I took these steps

  1. Implement hook_form_alter()

    • Add a new form element checkbox
    • hide() the original form element
    • Add a new validation callback
  2. Created the validation function

    • calculate the desired filter input
    • use form_set_value() on original filter element
    function hook_form_alter($form, $form_state, $form_id) {
        if (checkIfItIsTheDesiredForm()) {
            hide($form['originalFilter']);
            $form['newCheckbox'] = array(
                '#type'         => 'checkbox',
                '#title'        => 'Boyfriend',
                '#return_value' => 1,
                '#default_value' => 0,
            );
            $form['#validate'] = array_merge(array('manipulateFilterLogic'),$form['#validate']);
        }
    }
    function manipulateFilterLogic ($form, $form_state) {
        if (checkIfItIsTheDesiredForm()) {
            $filterValue =  ($form_state['input']['newCheckbox']) ? 1 : 'All';
            form_set_value($form['originalFilter'],$filterValue, $form_state);
        }
    }
2
  • This worked for me after I changed the functions to pass form and form_state by reference as - function hook_form_alter(&$form, &$form_state, $form_id), and function manipulateFilterLogic ($form, &$form_state)
    – ankles
    Commented Jun 21, 2016 at 15:05
  • Thanks Tobias this is great! Multiple form validators is a nice way to do it. Thanks for the pointer @ankles.
    – AndraeRay
    Commented Jun 4, 2017 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.