function form_type_checkboxes_value

7.x form_type_checkboxes_value($element, $input = FALSE)
6.x form_type_checkboxes_value($form, $edit = FALSE)
8.x form_type_checkboxes_value($element, $input = FALSE)

Determines the value for a checkboxes form element.


$element: The form element whose value is being populated.

$input: The incoming input to populate the form element. If this is FALSE, the element's default value should be returned.

Return value

The data that will appear in the $element_state['values'] collection for this element. Return nothing to use the default.

Related topics


drupal/includes/, line 2333
Functions for form and batch generation and processing.


function form_type_checkboxes_value($element, $input = FALSE) {
  if ($input === FALSE) {
    $value = array();
    $element += array('#default_value' => array());
    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; see drupal_form_submit(). 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 $_POST are strings.
    foreach ($input as $key => $value) {
      if (!isset($value)) {
    return drupal_map_assoc($input);
  else {
    return array();