function Checkbox::processCheckbox

Same name in other branches
  1. 9 core/lib/Drupal/Core/Render/Element/Checkbox.php \Drupal\Core\Render\Element\Checkbox::processCheckbox()
  2. 8.9.x core/lib/Drupal/Core/Render/Element/Checkbox.php \Drupal\Core\Render\Element\Checkbox::processCheckbox()
  3. 11.x core/lib/Drupal/Core/Render/Element/Checkbox.php \Drupal\Core\Render\Element\Checkbox::processCheckbox()

Sets the #checked property of a checkbox element.

File

core/lib/Drupal/Core/Render/Element/Checkbox.php, line 110

Class

Checkbox
Provides a form element for a single checkbox.

Namespace

Drupal\Core\Render\Element

Code

public static function processCheckbox(&$element, FormStateInterface $form_state, &$complete_form) {
    $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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.