Set form exposed filter default value
Asked Answered
B

6

5

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?

Baxy answered 4/2, 2014 at 11:56 Comment(0)
S
5

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.

Submerse answered 4/2, 2014 at 14:25 Comment(2)
You're right. But in addition to this code needs $view->exposed_input['field_producer_tid'] = "All"; Together they bring result. Thank you for help =) !!!Baxy
Another way: Remember the last selection = true + $view->filter['field_producer_tid']->value = "All"; in hook_views_pre_buildBaxy
K
3

Just in case that you (like me at first) overread Df.fpm's comment on botanic_spark's answer, the complete and working answer (as of 7.32) is as follows:

function YOUR_MODULE_views_pre_build(&$view) {
  if ($view->name == 'VIEW_NAME') {
    $view->filter['FIELD_ID']->value = "All";
    $view->exposed_input['FIELD_ID'] = "All";
  }
}

Only with the addition of the second line it will work!

Kerguelen answered 7/11, 2014 at 17:42 Comment(0)
R
2

The above solution will not allow you to switch between filters if it is a ajax view. I found this solution, better solution might be available but this will work.

/**
 * Implements hook_form_alter().
 */
function HOOK_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'views_exposed_form' && $form_state['view']->name =='VIEW_NAME') {
    $q = drupal_get_query_parameters();
    $form_state['input']['FIELD_ID'] = 'ALL';
    if($q['FIELD_ID']) {
    $form_state['input']['field_7_step_refernce_tid'] = $q['FIELD_ID'];
    }
  }
} 
Rosebud answered 9/12, 2015 at 10:32 Comment(1)
According to @Salmon this answer works, but it's also giving the following error: Notice: Undefined index at the line that reads if($q['field_location_tid']) {.Booster
E
1

I did manage to get this working by doing the following:

I changed the filter status value,

$view->display[$view->current_display]->handler->handlers['filter']['status']->value

/**
 * Implements hook_views_pre_build().
 */
function my_module_views_pre_build(&$view) {
  // Check if the current view is the orders view.
  if ($view->name == 'commerce_backoffice_orders') {
    if($view->current_display == 'page_1') {
      if (empty($view->display[$view->current_display]->handler->handlers['filter']['status']->value)) {
        $view->display[$view->current_display]->handler->handlers['filter']['status']->value = 'pending';
      }
    }
    elseif ($view->current_display == 'page_2') {
      if (empty($view->display[$view->current_display]->handler->handlers['filter']['status']->value)) {
        $view->display[$view->current_display]->handler->handlers['filter']['status']->value = 'processing';
      }
    }
  }
}
Enrage answered 3/11, 2017 at 13:31 Comment(0)
B
1

This solution is working on Drupal >= 8

function YOURMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form['#id'] == 'views-exposed-form-my-view-display') {
    $request = \Drupal::request();
    if (is_null($request->get('EXPOSED_FILTER_FIELD_MACHINE_NAME'))) {
      $form_state->setUserInput(['EXPOSED_FILTER_FIELD_MACHINE_NAME' => 'DEFAULT_VALUE']);
    }
  }
}
Bagley answered 16/1, 2020 at 13:19 Comment(0)
M
0

This worked better for me:

default values can be passed in as URL query parameters. For example if the filter has a single value:

  • /path/to/view?field_id=defaultvalue

If the filter can have multiple values:

  • /path/to/view?field_id%5B%5D=defaultvalue
Manlove answered 13/11, 2014 at 22:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.