Commit 84311312 authored by focus13's avatar focus13 Committed by Rick Hawkins
Browse files

Issue #2947281 by focus13: Single on/off checkbox is always checked

parent 7f85745e
......@@ -1110,8 +1110,36 @@ Title Desc|Z -> A</pre> Leave the replacement text blank to remove an option alt
// Use filter label as checkbox label.
$form[$field_id]['#title'] = $filters[$label]->options['expose']['label'];
$form[$field_id]['#return_value'] = 1;
$form[$field_id]['#type'] = 'checkbox';
// Views populates missing values in $form_state['input'] with the
// defaults and a checkbox does not appear in $_GET (or $_POST) so it
// will appear to be missing when a user submits a form. Because of
// this, instead of unchecking the checkbox value will revert to the
// default. More, the default value for select values is reused which
// results in the checkbox always checked. So we need to add a form
// element to see whether the form is submitted or not and then we
// need to look at $_GET directly to see whether the checkbox is
// there. For security reasons, we must not copy the $_GET value.
// First, let's figure out a short name for the signal element and
// then add it.
if (empty($signal)) {
for ($signal = 'a'; isset($form[$signal]); $signal++);
// This is all the signal element needs.
$form[$signal]['#type'] = 'hidden';
}
$input = $form_state->getUserInput();
$value = \Drupal::request()->query->get($field_id);
$checked = isset($input[$signal]) ? isset($value) : $form[$field_id]['#default_value'];
// For security, we check if value is valid and exist.
if ($checked) {
if (!in_array($value, array_keys($form[$field_id]['#options']))) {
$checked = FALSE;
}
}
// Now we know whether the checkbox is checked or not, set #value
// accordingly.
$form[$field_id]['#value'] = $checked ? $value : 0;
break;
case 'bef':
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment