Radios.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Drupal\Core\Render\Element;
  3. use Drupal\Core\Form\FormStateInterface;
  4. use Drupal\Component\Utility\Html as HtmlUtility;
  5. /**
  6. * Provides a form element for a set of radio buttons.
  7. *
  8. * Properties:
  9. * - #options: An associative array, where the keys are the returned values for
  10. * each radio button, and the values are the labels next to each radio button.
  11. *
  12. * Usage example:
  13. * @code
  14. * $form['settings']['active'] = array(
  15. * '#type' => 'radios',
  16. * '#title' => $this->t('Poll status'),
  17. * '#default_value' => 1,
  18. * '#options' => array(0 => $this->t('Closed'), 1 => $this->t('Active')),
  19. * );
  20. * @endcode
  21. *
  22. * @see \Drupal\Core\Render\Element\Checkboxes
  23. * @see \Drupal\Core\Render\Element\Radio
  24. * @see \Drupal\Core\Render\Element\Select
  25. *
  26. * @FormElement("radios")
  27. */
  28. class Radios extends FormElement {
  29. use CompositeFormElementTrait;
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function getInfo() {
  34. $class = get_class($this);
  35. return [
  36. '#input' => TRUE,
  37. '#process' => [
  38. [$class, 'processRadios'],
  39. ],
  40. '#theme_wrappers' => ['radios'],
  41. '#pre_render' => [
  42. [$class, 'preRenderCompositeFormElement'],
  43. ],
  44. ];
  45. }
  46. /**
  47. * Expands a radios element into individual radio elements.
  48. */
  49. public static function processRadios(&$element, FormStateInterface $form_state, &$complete_form) {
  50. if (count($element['#options']) > 0) {
  51. $weight = 0;
  52. foreach ($element['#options'] as $key => $choice) {
  53. // Maintain order of options as defined in #options, in case the element
  54. // defines custom option sub-elements, but does not define all option
  55. // sub-elements.
  56. $weight += 0.001;
  57. $element += [$key => []];
  58. // Generate the parents as the autogenerator does, so we will have a
  59. // unique id for each radio button.
  60. $parents_for_id = array_merge($element['#parents'], [$key]);
  61. $element[$key] += [
  62. '#type' => 'radio',
  63. '#title' => $choice,
  64. // The key is sanitized in Drupal\Core\Template\Attribute during output
  65. // from the theme function.
  66. '#return_value' => $key,
  67. // Use default or FALSE. A value of FALSE means that the radio button is
  68. // not 'checked'.
  69. '#default_value' => isset($element['#default_value']) ? $element['#default_value'] : FALSE,
  70. '#attributes' => $element['#attributes'],
  71. '#parents' => $element['#parents'],
  72. '#id' => HtmlUtility::getUniqueId('edit-' . implode('-', $parents_for_id)),
  73. '#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL,
  74. // Errors should only be shown on the parent radios element.
  75. '#error_no_message' => TRUE,
  76. '#weight' => $weight,
  77. ];
  78. }
  79. }
  80. return $element;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
  86. if ($input !== FALSE) {
  87. // When there's user input (including NULL), return it as the value.
  88. // However, if NULL is submitted, FormBuilder::handleInputElement() will
  89. // apply the default value, and we want that validated against #options
  90. // unless it's empty. (An empty #default_value, such as NULL or FALSE, can
  91. // be used to indicate that no radio button is selected by default.)
  92. if (!isset($input) && !empty($element['#default_value'])) {
  93. $element['#needs_validation'] = TRUE;
  94. }
  95. return $input;
  96. }
  97. else {
  98. // For default value handling, simply return #default_value. Additionally,
  99. // for a NULL default value, set #has_garbage_value to prevent
  100. // FormBuilder::handleInputElement() converting the NULL to an empty
  101. // string, so that code can distinguish between nothing selected and the
  102. // selection of a radio button whose value is an empty string.
  103. $value = isset($element['#default_value']) ? $element['#default_value'] : NULL;
  104. if (!isset($value)) {
  105. $element['#has_garbage_value'] = TRUE;
  106. }
  107. return $value;
  108. }
  109. }
  110. }