So, documentation indicates that 'checkboxes' element is making use of '#default_values' property. There are multiple threads indicating that it's possible, like:
- Checkboxes: how do I set the default values or show the user defined values using Form API?
- How to check a checkbox by default in forms api
And many more.
However, it doesn't work for me, although I'm currently trying it in Views custom field handler, but I think it shouldn't matter. I'm looking at the code of Drupal core to see how it handles #default_value for 'checkboxes' element:
https://github.com/drupal/drupal/blob/7.x/includes/form.inc#L3290-L3351
So, nothing from the 'checkboxes' #default_value
is being put into created 'checkbox' elements. But there is more - 'checkbox' type doesn't even use #default_value
property to decide if it is checked or not, but rather #value
property, and that one is empty if form has not been submitted.
Let's read description of #default_value
from documentation again:
The value of the form element that will be displayed or selected initially if the form has not been submitted yet.
And let's look at the code not having any expected reference to #default_value
again:
function form_process_checkbox($element, $form_state) {
$value = $element['#value'];
$return_value = $element['#return_value'];
// On form submission, the #value of an available and enabled checked
// checkbox is #return_value, and the #value of an available and enabled
// unchecked checkbox is integer 0. On not submitted forms, and for
// checkboxes with #access=FALSE or #disabled=TRUE, the #value is
// #default_value (integer 0 if #default_value is NULL). Most of the time,
// a string comparison of #value and #return_value is sufficient for
// determining the "checked" state, but a value of TRUE always means checked
// (even if #return_value is 'foo'), and a value of FALSE or integer 0 always
// means unchecked (even if #return_value is '' or '0').
if ($value === TRUE || $value === FALSE || $value === 0) {
$element['#checked'] = (bool) $value;
}
else {
// Compare as strings, so that 15 is not considered equal to '15foo', but 1
// is considered equal to '1'. This cast does not imply that either #value
// or #return_value is expected to be a string.
$element['#checked'] = ((string) $value === (string) $return_value);
}
return $element;
}
/**
* Processes a checkboxes form element.
*/
function form_process_checkboxes($element) {
$value = is_array($element['#value']) ? $element['#value'] : array();
$element['#tree'] = TRUE;
if (count($element['#options']) > 0) {
if (!isset($element['#default_value']) || $element['#default_value'] == 0) {
$element['#default_value'] = array();
}
$weight = 0;
foreach ($element['#options'] as $key => $choice) {
// Integer 0 is not a valid #return_value, so use '0' instead.
// @see form_type_checkbox_value().
// @todo For Drupal 8, cast all integer keys to strings for consistency
// with form_process_radios().
if ($key === 0) {
$key = '0';
}
// Maintain order of options as defined in #options, in case the element
// defines custom option sub-elements, but does not define all option
// sub-elements.
$weight += 0.001;
$element += array($key => array());
$element[$key] += array(
'#type' => 'checkbox',
'#title' => $choice,
'#return_value' => $key,
'#default_value' => isset($value[$key]) ? $key : NULL,
'#attributes' => $element['#attributes'],
'#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL,
'#weight' => $weight,
);
}
}
return $element;
So, it doesn't work, and looking at the code it shouldn't work. What am I missing?
value_form
method to be exact. It's possible that Views is doing some custom theming and makes it not working, or something like that, but I'm just interested how it actually works when it works.'#default_value' => ['key1', 'key3'],
This will cause the checkboxes that have the keys 'key1' and 'key3' to be checked.#default_value
and#checked
:) .