Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set form exposed filter default value

Ajax form. Exposed filter with -Any-. In hook_form_alter() i write:

if ($form_id == 'views_exposed_form') {
    if ($form_state['view']->name == 'machinery') {
        $form['field_producer_tid']['#options']['All'] = t('-All-');
    }
    $form['field_producer_tid']['#default_value'] = "All";
    dsm($form);
}

But default value is the second <option> from select list. Always. Any value which i assign is ignored. How should i set default value?

like image 338
Df.fpm Avatar asked Feb 04 '14 11:02

Df.fpm


1 Answers

According to https://drupal.org/node/1239868 you will have to use some other hook. You can try

/**
 * Implements hook_views_pre_build().
 */
function YOUR_MODULE_views_pre_build(&$view) {
  if ($view->name == 'machinery') {
    $view->filter['field_producer_tid']->value = "All";
  }
}

This way, you will be able to choose default value.

like image 122
botanic_spark Avatar answered Oct 20 '22 07:10

botanic_spark