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.

10

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 :)

|improve this answer|||||
  • Good solution. You should mark this as accepted. – mpdonadio May 15 '11 at 16:02
  • Thanks. I would, except that I'm not allowed to mark my own solution as accepted within 2 days of posting the question! – Tom Kirkpatrick May 15 '11 at 16:28
  • In fact if you put an ALL value with an wmpty key, you don't need to unset it after: array("" => t("- All -") – tostinni Jun 13 '11 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.

|improve this answer|||||
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();
  }
}
|improve this answer|||||
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.

|improve this answer|||||
  • 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. – Tom Kirkpatrick May 14 '11 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.

|improve this answer|||||
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.

|improve this answer|||||
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', []);
  }
}
|improve this answer|||||
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.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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