I searched for an entire day on this issue and found no working solutions that affect the "initial query" of an exposed form in Drupal 7.
I have a simple checkbox exposed as a select list; the options are All, 1, or 0. It defaults to All on default. I'm trying to change it to 0. With that said, I was able to successfully set the default value of my exposed form (doesn't affect the initial query) and rename the options with a custom module:
function MODULE_form_alter(&$form,&$form_state,$form_id) {
//dpm($form);
if($form_id == 'views_exposed_form') {
$form['field_checkbox']['#default_value'] = '0';
$form['field_checkbox']['#options']['All'] = t('Both');
$form['field_checkbox']['#options']['0'] = t('Unchecked');
$form['field_checkbox']['#options']['1'] = t('Checked');
}
}
I tried to interpret merlinofchaos' advice in the only relevant advice I could find on this (9 year old thread) and might not even be relevant to D7.
function MODULE_NAME_form_alter(&$form, $form_state, $form_id) {
//print $form['#id'];
if($form['#id'] == 'EXPOSED_FORM_NAME') {
if (!isset($_GET['EXPOSED_FILTER_NAME'])) {
$form_state['input']['EXPOSED_FILTER_NAME'] = '0';
}
}
}
Though the above code changes the $form_state['input']['EXPOSED_FILTER_NAME']
variable from "All" to "0", it doesn't change the default query. I have dpm'd both $form
and $form_state
, but I don't know what needs to be changed. Any advice would be extremely appreciated!