5

I have a Views exposed filter, that allows multiple items to be selected. It's based on the 'Allowed values' for a multiselect CCK field.

The exposed filter comes out looking like this:

Option 1
Option 2
Option 3

I need to add an 'All' option to the top, so that it would be:

All
Option 1
Option 2
Option 3

Clicking the All option should have the same effect as holding Ctrl and clicking on Options 1, 2 and 3. ie. return all nodes that have a value of Option 1, Option 2 or Option 3 for this field.

How can I add such an option to my widget? I'm sure it could be done through form_alter somehow, but I can't suss out how.

8 Answers 8

11

OK... I think I may have cracked it.

function mymodule_form_alter(&$form, $form_state, $form_id) { 
  if ($form_state['view']->name == 'myview') {
    // Add our custom All buttons.
    $form['myfield']['#options'] = array("-ALL-" => t("- All -")) + $form['myfield']['#options'];
    array_unshift($form['#submit'], 'mymodule_exposed_form_submit');
  }
}

function mymodule_exposed_form_submit(&$form, &$form_state) { 
  if (!empty($form_state['values']['myfield']['-ALL-'])) {
    $options = $form['myfield']['#options'];
    unset($options['-ALL-']);
    $form_state['values']['myfield'] = $options;
  }
}

So basically I add in my 'All' item at the top of the select. Then I add my own custom submit handler before Views' one.

In my submit handler, I check for the presence of the All option, and if its found, I adjust the selected options so that all options are selected except for the All option.

Seem to work :)

1
  • If you put an ALL value with an empty key, you don't need to unset it after: array("" => t("- All -").
    – tostinni
    Jun 13, 2011 at 16:20
2

You could simply add same not exposed filter with your options:

Option 1
Option 2
Option 3

Then "All" option in exposed filter will show results only for those three options.

2

In my case (Views exposed filter of taxonomy terms) I had to unselect all options in order to get -All-. Not sure why it works that way for me.

function mymodule_exposed_form_submit(&$form, &$form_state) { 
  if (!empty($form_state['values']['myfield']['-ALL-'])) {
    $form_state['values']['myfield'] = array();
  }
}
1
  • You saved my day, it worked like a charm for taxonomy term exposed filter. May 26, 2020 at 22:42
1

I think that you cannot do this by default, however, you can use a module that was created specifically for to improved the exposed filters. It is called Better Exposed Filters.

1
  • Better Exposed filters does not allow for this specific adjustment. It can be done in a custom module, which is what I am trying to do - hence my reference to using hook_form_alter to achieve it. My question is more about exactly how I would do such a thing. May 14, 2011 at 17:16
1

I have done two very ugly solutions.

Assuming you have a default to print out all values, you can hack in the option in your display output template (the one based on views-view.tpl.php). An example

<?php if ($attachment_before): ?>
  <div class="attachment attachment-before">
  <?php $attachment_before = str_replace('<div class="view-content">',                                     
   '<div class="view-content"><div class="views-summary views-summary-unformatted"><a href="/glossary/all">All</a></div>', $attachment_before); ?>

    <?php print $attachment_before; ?>
  </div>
<?php endif; ?>

The other method essentially does the same thing with jQuery.

1

When an exposed filter is designated as multiple and not-optional, selecting all or none of the options gives the same results. One option would be to mention that in the filter help text. Another option is to install Better Exposed Filters and enable the select all/none link which, while not changing the functionality of the filter, will give your users a more intuitive interface.

1

Tom Kirkpatrick was correct, however, instead of adding all the options, you can also remove all the options so the form state value is empty. This also shows all values by default and is a bit cleaner:

function mymodule_form_alter(&$form, $form_state, $form_id) { 
  if ($form_state['view']->name == 'myview') {
    // Add our custom All buttons.
    $form['myfield']['#options'] = array("-ALL-" => t("- All -")) + $form['myfield']['#options'];
    array_unshift($form['#submit'], 'mymodule_exposed_form_submit');
  }
}

function mymodule_exposed_form_submit(&$form, &$form_state) { 
  $myfield_values = $form_state->getValue('myfield');
  if (!empty($myfield_values) && isset($myfield_values['-ALL-'])) {
    $form_state->set('myfield', []);
    $form_state->setValue('myfield', []);
  }
}
0

At least for Drupal 8 (I don't know if it applies for D7 also), Better Exposed Filters gives you the possibility to "Add select all/none links" to your exposed filter. BEF's Version I'm using: 8.x-3.0

To add this option to your exposed filter, go to your View, access the Advanced panel > Better Exposed Filters > Settings and under each exposed filter you will find a collapsed pannel labeled "MORE OPTIONS FOR "[FIELD_NAME]" (FILTER LABEL: "[LABEL]")".

Select the checkbox labeled "Add a "Select All/None" to:

"Add a "Select All/None" link when rendering the exposed filter using checkboxes. If this option is disabled, edit the filter and check the "Allow multiple selections"."

!The issue I have noticed with this "All" option selected is that it keeps refreshing the page when the form has Auto Submit on. But, if you don't need that functionality, seems to be working like a charm.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.