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
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:
Comments
Comment #3
MustangGB CreditAttribution: MustangGB commentedComment #4
renatogI 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
Comment #6
renatogMoved to the dev branch
Thank you so much for your contribution, MustangGB
Comment #8
albertski CreditAttribution: albertski at Xeno Media, Inc. commentedI 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.