It is currently not possible to by default pre-select another option than either 'All' ('Optional' flag is checked) or the first option in the options list ("Force single" is checked) for an exposed filter of type views_handler_filter_in_operator.

I currently have overridden value_form in a subclass of views_handler_filter_in_operator to preselect the option we want (hardcoded, see code below) but it would be nice if a configurable (in the views GUI) default option (when "Force single" is checked), or multiple configurable default options would be available by default in views_handler_filter_in_operator.

 /**
   * Re-implementation of views_handler_filter_in_operator::value_form(), to
   * override the option selected by default
   */
  function value_form(&$form, &$form_state) {
    // parent's value_form alters form_state but we want to check the original form_state later on
    // so make a copy of it
    $orig_form_state = $form_state;
    parent::value_form(&$form, &$form_state);
    
    $default_value = 'mydefaultvalue';
    
    $form['value']['#default_value'] = $default_value;
    
    if (!empty($form_state['exposed'])) {
      $identifier = $this->options['expose']['identifier'];
      
      // original form state is checked here
      // as parent's value_form() already altered it to set the first option as the default
      if(!isset($orig_form_state['input'][$identifier])) {
        $form_state['input'][$identifier] = $default_value;
      }
    }
  }

Comments

Cyberwolf’s picture

Title: Allow to set by default selected option(s) selected for views_handler_filter_in_operator » Allow to set by default selected option(s) for views_handler_filter_in_operator
merlinofchaos’s picture

Status: Active » Closed (won't fix)

This is only true if you don't also have "Limit list to selected items" because at that point, we'd have to add *another* selector to determine the default. However, if Limit list is not selected, then whatever is selected in the filter itself will be the default for the exposed filter.

Cyberwolf’s picture

Status: Closed (won't fix) » Active

if Limit list is not selected, then whatever is selected in the filter itself will be the default for the exposed filter

That doesn't seem to work here though. The 'Optional' flag is checked, and the default value of the filter is always the first "Any" option.

It turns out that I selected the wrong version here, we're using views 6.x-2.10, not the 6.x-3 branch. Was the functionality you describe added in views in the 6.x-3 branch?

dawehner’s picture

Status: Active » Postponed

No it wasn't added to views 3.x.

Noone worked on this issue so i guess it can be marked as postponed. Reopen if you have a patch

merlinofchaos’s picture

Category: feature » task
Status: Postponed » Active

Making an active task. It is unassigned meaning it has no one to work on it.

MustangGB’s picture

Status: Active » Closed (won't fix)