0

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!

1

Try this out with hook_views_pre_build

/**
 * Implements hook_views_pre_build().
 */
function MODULE_NAME_views_pre_build(&$view) {
    if ($view->name == 'VIEW_MACHINE_NAME') {
        //- Set the exposed filter to new value 
        $view->filter['EXPOSED_FILTER_NAME']->value = '0';
    }
}
|improve this answer|||||
  • You did it! views_pre_build(), interesting... Thank you very much for your help! – WebMW Oct 4 '18 at 1:40
  • You are so welcome, good luck – berramou Oct 4 '18 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.