You are here

public static function Checkboxes::valueCallback in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 101
Contains \Drupal\Core\Render\Element\Checkboxes.

Class

Checkboxes
Provides a form element for a set of checkboxes.

Namespace

Drupal\Core\Render\Element

Code

public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
  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. 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]);
      }
    }
    return array_combine($input, $input);
  }
  else {
    return array();
  }
}