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
Comment #1
Cyberwolf CreditAttribution: Cyberwolf commentedComment #2
merlinofchaos CreditAttribution: merlinofchaos commentedThis 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.
Comment #3
Cyberwolf CreditAttribution: Cyberwolf commentedThat 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?
Comment #4
dawehnerNo 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
Comment #5
merlinofchaos CreditAttribution: merlinofchaos commentedMaking an active task. It is unassigned meaning it has no one to work on it.
Comment #6
MustangGB CreditAttribution: MustangGB commented