0

I'm trying to get exposed filter selected value in hook_form_alter().

myModule_form_alter(&$form, $form_state, $form_id) {
     if ($form['#id'] == 'views-exposed-form-view-id-display-id'){
          var_dump($form['exposed_element']['#options']);
     }
}

i tried with also

$form['exposed_element']['values'],
$form['values']['exposed_element']
$form['exposed_element']

But none of them giving me selected option value.

Any help/suggestion would be greatly appreciated.

EDIT

For more clarification

function myModule_form_alter(&$form, $form_state, $form_id) {
    switch ($form_id) {
        case 'views_exposed_form':
            switch($form['#id'])
            {
                case 'views-exposed-form-soldproduct-page-1':
                       var_dump($form['exposed_element']['#options']);
                break;
            }
        break;
    }
}

Where soldproduct is my view name and page-1 is the view display id.

1

1 Answer 1

3

First of all, look up the exact filter's name. To do that open your view's builder UI and go to this exposed filter's settings page; look for the "Filter identifier" text field.

After that, you can get the filter's currently selected value in hook_form_alter():

function myModule_form_alter(&$form, $form_state, $form_id) {
    if ($form_id == 'views_exposed_form'){
        $view = &$form_state['view'];
        if ($view->name == 'your view name' && $view->current_display == 'page_1') {
            var_dump($view->exposed_input['your filter identifier']);
            // OR
            var_dump($form_state['input']['your filter identifier']);
        }
    }
}
1
  • Thanks a lot. It works. $form_state['input']['your filter identifier'] works in all situation but $view->exposed_input['your filter identifier'] only works when filter has some applied value means not empty at very first time. Nov 2, 2011 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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