W3cubDocs

/Drupal 8

public static function Checkboxes::valueCallback

public static Checkboxes::valueCallback(&$element, $input, FormStateInterface $form_state)

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 96

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();
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!Element!Checkboxes.php/function/Checkboxes::valueCallback/8.1.x