Closed (fixed)
Project:
Views (for Drupal 7)
Version:
7.x-3.1
Component:
Views Data
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
9 Feb 2012 at 18:19 UTC
Updated:
23 Feb 2012 at 18:50 UTC
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
Comment #1
merlinofchaos commentedThe code you have ought to work, but you're missing one really crucial bit.
Should be
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.
Comment #2
vishalkhialani commented@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