Problem/Motivation

When an exposed select filter is attached to a view it's default value can be set, and works as expected.
However if "Exposed form in block" is used this select filter will now default to - Any - rather than the desired default value.

This isn't noticed for attached filters because although the default value is still set to - Any -, but then overridden by field handlers which set $form_state['input'].

For block filters this becomes an issue because it uses a cached $form, so the field handlers don't run.

Proposed resolution

Either:

1) Run field handlers on cached forms.

Or:

2) Set the default value correctly for select fields.

I suggest that (1) is less desired as it negates a lot of the point in using a cache, so arguably (2) is the way to go.

This is what (2) might look like:

views_handler_filter.inc

  ...
  public function exposed_translate(&$form, $type) {
    ...
    if ($type == 'value' && empty($this->always_required) && empty($this->options['expose']['required']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
      $any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? t('<Any>') : t('- Any -');
      $form['#options'] = array('All' => $any_label) + $form['#options'];
-     $form['#default_value'] = 'All';
+     if (empty($form['#default_value'])) {
+       $form['#default_value'] = 'All';
+     }
    }
    ...
  }
  ...

Setting as needs work so someone can try rolling the above into a patch.

Issue fork views-3185587

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

MustangGB created an issue. See original summary.

MustangGB’s picture

Status: Needs work » Needs review
renatog’s picture

Status: Needs review » Reviewed & tested by the community

I totally agree with @MustangGB. On that case will be $form['#default_value'] = 'All' only if necessary

Moving to RTBC. It really makes sense. Thanks a lot MustangGB, good catch

  • RenatoG committed 519ef02 on 7.x-3.x authored by MustangGB
    Issue #3185587 by MustangGB, RenatoG: Select filters in exposed blocks...
renatog’s picture

Status: Reviewed & tested by the community » Fixed
Parent issue: » #3118500: Plan for Views 7.x-3.25

Moved to the dev branch

Thank you so much for your contribution, MustangGB

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

albertski’s picture

I have a view with exposed filters where I have a filter of countries. On the initial load of the view, it shows empty results. As soon I submit the form, the items appear. When I set `$form['#default_value'] = 'All';` for everyone, it fixes the issue. I'm using View 7.x-3.25. Not sure what is going on because the patch does make sense to me but for some reason it is causing issues for me. I wonder if anyone else is experiencing this.