public static function Checkboxes::valueCallback in Drupal 10
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/Render/Element/Checkboxes.php \Drupal\Core\Render\Element\Checkboxes::valueCallback()
- 9 core/lib/Drupal/Core/Render/Element/Checkboxes.php \Drupal\Core\Render\Element\Checkboxes::valueCallback()
Determines how user input is mapped to an element's #value property.
Parameters
array $element: An associative array containing the properties of the element.
mixed $input: The incoming input to populate the form element. If this is FALSE, the element's default value should be returned.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
mixed The value to assign to the element.
Overrides FormElement::valueCallback
File
- core/
lib/ Drupal/ Core/ Render/ Element/ Checkboxes.php, line 109
Class
- Checkboxes
- Provides a form element for a set of checkboxes.
Namespace
Drupal\Core\Render\ElementCode
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
if ($input === FALSE) {
$value = [];
$element += [
'#default_value' => [],
];
foreach ($element['#default_value'] as $key) {
$value[$key] = $key;
}
return $value;
}
elseif (is_array($input)) {
// Programmatic form submissions use NULL to indicate that a checkbox
// should be unchecked. We therefore remove all NULL elements from the
// array before constructing the return value, to simulate the behavior
// of web browsers (which do not send unchecked checkboxes to the server
// at all). This will not affect non-programmatic form submissions, since
// all values in \Drupal::request()->request are strings.
// @see \Drupal\Core\Form\FormBuilderInterface::submitForm()
foreach ($input as $key => $value) {
if (!isset($value)) {
unset($input[$key]);
}
}
// Because the disabled checkboxes don't receive their input from the
// form submission, we use their default value.
if (!empty($element['#default_value'])) {
foreach ($element['#default_value'] as $key) {
if (!empty($element[$key]['#disabled'])) {
$input[$key] = $key;
}
}
}
return array_combine($input, $input);
}
else {
return [];
}
}