Commit 77d0af67 authored by catch's avatar catch
Browse files

fix: #2839344 Broken aria-describedby in radios and checkboxes

By: Thew
By: mgifford
By: andrewmacpherson
By: amanire
By: jontynewman
By: jacine
By: solideogloria
By: dcam
By: liam morland
By: smustgrave
(cherry picked from commit 2e855eca)
parent d797ca8c
Loading
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -65,6 +65,13 @@ public static function processCheckboxes(&$element, FormStateInterface $form_sta
        $element['#default_value'] = [];
      }
      $weight = 0;
      $child_attributes = $element['#attributes'];
      // Prevent child elements from inheriting an aria-describedby attribute.
      // The individual children won't have descriptions and the attribute will
      // be invalid.
      if (isset($child_attributes['aria-describedby'])) {
        unset($child_attributes['aria-describedby']);
      }
      foreach ($element['#options'] as $key => $choice) {
        // Integer 0 is not a valid #return_value, so use '0' instead.
        // @see \Drupal\Core\Render\Element\Checkbox::valueCallback().
@@ -91,7 +98,7 @@ public static function processCheckboxes(&$element, FormStateInterface $form_sta
          '#title' => $choice,
          '#return_value' => $key,
          '#default_value' => $default_value,
          '#attributes' => $element['#attributes'],
          '#attributes' => $child_attributes,
          '#ajax' => $element['#ajax'] ?? NULL,
          // Errors should only be shown on the parent checkboxes element.
          '#error_no_message' => TRUE,
+8 −1
Original line number Diff line number Diff line
@@ -60,6 +60,13 @@ public function getInfo() {
  public static function processRadios(&$element, FormStateInterface $form_state, &$complete_form) {
    if (count($element['#options']) > 0) {
      $weight = 0;
      $child_attributes = $element['#attributes'];
      // Prevent child elements from inheriting an aria-describedby attribute.
      // The individual children won't have descriptions and the attribute will
      // be invalid.
      if (isset($child_attributes['aria-describedby'])) {
        unset($child_attributes['aria-describedby']);
      }
      foreach ($element['#options'] as $key => $choice) {
        // Maintain order of options as defined in #options, in case the element
        // defines custom option sub-elements, but does not define all option
@@ -79,7 +86,7 @@ public static function processRadios(&$element, FormStateInterface $form_state,
          // Use default or FALSE. A value of FALSE means that the radio button
          // is not 'checked'.
          '#default_value' => $element['#default_value'] ?? FALSE,
          '#attributes' => $element['#attributes'],
          '#attributes' => $child_attributes,
          '#parents' => $element['#parents'],
          '#id' => HtmlUtility::getUniqueId('edit-' . implode('-', $parents_for_id)),
          '#ajax' => $element['#ajax'] ?? NULL,
+2 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $customiz
    $form['checkboxes'] = [
      '#type' => 'checkboxes',
      '#title' => 'Checkboxes',
      '#description' => 'Checkboxes description',
      '#options' => [
        0 => 'Zero',
        'foo' => 'Foo',
@@ -55,6 +56,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $customiz
    $form['radios'] = [
      '#type' => 'radios',
      '#title' => 'Radios',
      '#description' => 'Radios description',
      '#options' => [
        0 => 'Zero',
        'foo' => 'Foo',
+12 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ public function testFormElements(): void {
    $this->testOptions();
    $this->testRadiosChecked();
    $this->testWrapperIds();
    $this->testChildAttributes();
    $this->testButtonClasses();
    $this->testSubmitButtonAttribute();
    $this->testGroupElements();
@@ -138,6 +139,17 @@ protected function testWrapperIds(): void {
    }
  }

  /**
   * Tests checkboxes and radios child element attributes.
   */
  protected function testChildAttributes(): void {
    $this->drupalGet('form-test/checkboxes-radios');

    // Verify that the child elements don't have aria-describedby attributes.
    $this->assertSession()->elementsCount('xpath', "//input[@type='checkbox' and @aria-describedby]", 0);
    $this->assertSession()->elementsCount('xpath', "//input[@type='radio' and @aria-describedby]", 0);
  }

  /**
   * Tests button classes.
   */