0

So, documentation indicates that 'checkboxes' element is making use of '#default_values' property. There are multiple threads indicating that it's possible, like:

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?

7
  • It's always worked for me. If it's not working for you, maybe you could post the code you are having troubles with.
    – Jaypan
    Commented Nov 20, 2018 at 21:02
  • The code is in a custom views filter handler, 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. Commented Nov 20, 2018 at 21:09
  • You pass an array of the keys that should be checked. '#default_value' => ['key1', 'key3'], This will cause the checkboxes that have the keys 'key1' and 'key3' to be checked.
    – Jaypan
    Commented Nov 20, 2018 at 21:11
  • I know that. But where in core is the path between this #default_value and #checked :) . Commented Nov 20, 2018 at 21:12
  • I haven't looked directly into it since Drupal 6, but if I recall correctly, the checkboxes type has a #process handler that creates individual items of type checkbox, which Drupal then renders - I believe the default value is passed when creating the individual checkbox items.
    – Jaypan
    Commented Nov 20, 2018 at 21:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.