Checkbox.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace Drupal\Core\Render\Element;
  3. use Drupal\Core\Form\FormStateInterface;
  4. use Drupal\Core\Render\Element;
  5. /**
  6. * Provides a form element for a single checkbox.
  7. *
  8. * Properties:
  9. * - #return_value: The value to return when the checkbox is checked.
  10. *
  11. * Usage example:
  12. * @code
  13. * $form['copy'] = array(
  14. * '#type' => 'checkbox',
  15. * '#title' => $this->t('Send me a copy'),
  16. * );
  17. * @endcode
  18. *
  19. * @see \Drupal\Core\Render\Element\Checkboxes
  20. *
  21. * @FormElement("checkbox")
  22. */
  23. class Checkbox extends FormElement {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function getInfo() {
  28. $class = get_class($this);
  29. return [
  30. '#input' => TRUE,
  31. '#return_value' => 1,
  32. '#process' => [
  33. [$class, 'processCheckbox'],
  34. [$class, 'processAjaxForm'],
  35. [$class, 'processGroup'],
  36. ],
  37. '#pre_render' => [
  38. [$class, 'preRenderCheckbox'],
  39. [$class, 'preRenderGroup'],
  40. ],
  41. '#theme' => 'input__checkbox',
  42. '#theme_wrappers' => ['form_element'],
  43. '#title_display' => 'after',
  44. ];
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
  50. if ($input === FALSE) {
  51. // Use #default_value as the default value of a checkbox, except change
  52. // NULL to 0, because FormBuilder::handleInputElement() would otherwise
  53. // replace NULL with empty string, but an empty string is a potentially
  54. // valid value for a checked checkbox.
  55. return isset($element['#default_value']) ? $element['#default_value'] : 0;
  56. }
  57. else {
  58. // Checked checkboxes are submitted with a value (possibly '0' or ''):
  59. // http://www.w3.org/TR/html401/interact/forms.html#successful-controls.
  60. // For checked checkboxes, browsers submit the string version of
  61. // #return_value, but we return the original #return_value. For unchecked
  62. // checkboxes, browsers submit nothing at all, but
  63. // FormBuilder::handleInputElement() detects this, and calls this
  64. // function with $input=NULL. Returning NULL from a value callback means
  65. // to use the default value, which is not what is wanted when an unchecked
  66. // checkbox is submitted, so we use integer 0 as the value indicating an
  67. // unchecked checkbox. Therefore, modules must not use integer 0 as a
  68. // #return_value, as doing so results in the checkbox always being treated
  69. // as unchecked. The string '0' is allowed for #return_value. The most
  70. // common use-case for setting #return_value to either 0 or '0' is for the
  71. // first option within a 0-indexed array of checkboxes, and for this,
  72. // \Drupal\Core\Render\Element\Checkboxes::processCheckboxes() uses the
  73. // string rather than the integer.
  74. return isset($input) ? $element['#return_value'] : 0;
  75. }
  76. }
  77. /**
  78. * Prepares a #type 'checkbox' render element for input.html.twig.
  79. *
  80. * @param array $element
  81. * An associative array containing the properties of the element.
  82. * Properties used: #title, #value, #return_value, #description, #required,
  83. * #attributes, #checked.
  84. *
  85. * @return array
  86. * The $element with prepared variables ready for input.html.twig.
  87. */
  88. public static function preRenderCheckbox($element) {
  89. $element['#attributes']['type'] = 'checkbox';
  90. Element::setAttributes($element, ['id', 'name', '#return_value' => 'value']);
  91. // Unchecked checkbox has #value of integer 0.
  92. if (!empty($element['#checked'])) {
  93. $element['#attributes']['checked'] = 'checked';
  94. }
  95. static::setAttributes($element, ['form-checkbox']);
  96. return $element;
  97. }
  98. /**
  99. * Sets the #checked property of a checkbox element.
  100. */
  101. public static function processCheckbox(&$element, FormStateInterface $form_state, &$complete_form) {
  102. $value = $element['#value'];
  103. $return_value = $element['#return_value'];
  104. // On form submission, the #value of an available and enabled checked
  105. // checkbox is #return_value, and the #value of an available and enabled
  106. // unchecked checkbox is integer 0. On not submitted forms, and for
  107. // checkboxes with #access=FALSE or #disabled=TRUE, the #value is
  108. // #default_value (integer 0 if #default_value is NULL). Most of the time,
  109. // a string comparison of #value and #return_value is sufficient for
  110. // determining the "checked" state, but a value of TRUE always means checked
  111. // (even if #return_value is 'foo'), and a value of FALSE or integer 0
  112. // always means unchecked (even if #return_value is '' or '0').
  113. if ($value === TRUE || $value === FALSE || $value === 0) {
  114. $element['#checked'] = (bool) $value;
  115. }
  116. else {
  117. // Compare as strings, so that 15 is not considered equal to '15foo', but
  118. // 1 is considered equal to '1'. This cast does not imply that either
  119. // #value or #return_value is expected to be a string.
  120. $element['#checked'] = ((string) $value === (string) $return_value);
  121. }
  122. return $element;
  123. }
  124. }