so I have this exposed from and I want to change the default value when the view is displayed. I did search and found many articles on it but either they are for older versions of views or seem to be incomplete or maybe I am wrong somewhere.

Ref articles:
drupal.org/node/635242

http://drupal.stackexchange.com/questions/1812/change-the-default-select...

Most of them suggest using hook_form_alter but I think as we are using views hook form alter is called too late. How about hook_views_pre_build will that help ?

Any help will be much appreciated.

function modulename_form_alter(&$form, $form_state, $form_id) {

if($form['#id'] == 'name-of-the-views-form-displayed') {
 if (empty($_GET['label-of-the-filter']))  {
$form_state['input']['label-of-the-filter'] = 'default-value-for-filter';
 }
}
}

Comments

merlinofchaos’s picture

Category: task » support
Status: Active » Fixed

The code you have ought to work, but you're missing one really crucial bit.

function modulename_form_alter(&$form, $form_state, $form_id) {

Should be

function modulename_form_alter(&$form, &$form_state, $form_id) {

Without that, your changes to 'input' are not set.

Also be sure that the identifier (it's not the label) you are using is the correct one. There should be a value already in $form_state['input'] for it, so if that's not there it would be a sign you're looking at the wrong value.

vishalkhialani’s picture

Category: support » task

@merlinofchaos: thank you It worked

I feel so stupid I spend 5 hrs on this and it was the simple reference &$form_state

cheers,
Vishal

Status: Fixed » Closed (fixed)

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