Set Default Value for Views Exposed Filters

Public

Setting Default Value for Views Exposed Filters sometime could be pain in a ass if you have modified the predefined set of values in an exposed filter and you want to set default value of exposed filter which you have imposed. With this snippet you can set default value for a views exposed filter

Get raw version
php
  1. function MYMODULE_form_views_exposed_form_alter(&$form, &$form_state) {
  2. if (!isset($form_state['view']->exposed_input['<your_filter_field>'])) {
  3. // $form_state['view']->exposed_input only hold the value when you have applied any filter. Initially it doesn't hold any value. while $form_state['input'] hold the actual filter value to be applied.
  4. $form_state['input']['<filter_field>'] = '<default_value>';
  5. }
  6. }

Comments

Alauddin's picture

This works out of the box with views 3.

In Configure filter criterion,
select the operator 'is one off'
in options select the 'default value' you want...that's it.
and since you will have 'expose this filter' checked the user will be able to change to other options later.

msizec's picture

Thanks alauddin for the tips !!

javlens's picture

I have views 7.x-3.7+24-dev, and it's not showing me the 'default value'. I'm able to set the value view the $form_state = ['input'][''] = 'whatever';

However, the form doesn't refresh