diff --git a/core/modules/file/src/Element/ManagedFile.php b/core/modules/file/src/Element/ManagedFile.php index 2d076bfa6819389a7b2bfbb862e2c07dd00fdcd3..7b80af79ebfbda85e7b104ff3b86a994d950c3f9 100644 --- a/core/modules/file/src/Element/ManagedFile.php +++ b/core/modules/file/src/Element/ManagedFile.php @@ -320,6 +320,11 @@ public static function processManagedFile(&$element, FormStateInterface $form_st '#weight' => -10, '#error_no_message' => TRUE, ]; + + if (!empty($element['#description'])) { + $element['upload']['#attributes']['aria-describedby'] = $element['#id'] . '--description'; + } + if (!empty($element['#accept'])) { $element['upload']['#attributes'] = ['accept' => $element['#accept']]; } diff --git a/core/modules/file/tests/file_module_test/src/Form/FileModuleTestForm.php b/core/modules/file/tests/file_module_test/src/Form/FileModuleTestForm.php index e97d792b74fbba4927002640092053c13bbf346a..7db60a46a9602fb2a341a99d8d9b22bd31d7ac61 100644 --- a/core/modules/file/tests/file_module_test/src/Form/FileModuleTestForm.php +++ b/core/modules/file/tests/file_module_test/src/Form/FileModuleTestForm.php @@ -41,6 +41,7 @@ public function buildForm(array $form, FormStateInterface $form_state, $tree = T $form['nested']['file'] = [ '#type' => 'managed_file', '#title' => $this->t('Managed <em>@type</em>', ['@type' => 'file & butter']), + '#description' => $this->t('Upload a <em>@type</em> file', ['@type' => 'file & butter']), '#upload_location' => 'public://test', '#progress_message' => $this->t('Processing...'), '#extended' => (bool) $extended, diff --git a/core/modules/file/tests/src/Functional/FileManagedFileElementTest.php b/core/modules/file/tests/src/Functional/FileManagedFileElementTest.php index 2d02bc4863701061f0a10c942186a19987874910..9cc2d3fd949c0608a259863b42653aac7da46ee3 100644 --- a/core/modules/file/tests/src/Functional/FileManagedFileElementTest.php +++ b/core/modules/file/tests/src/Functional/FileManagedFileElementTest.php @@ -4,6 +4,7 @@ namespace Drupal\Tests\file\Functional; +use Drupal\Component\Utility\Html; use Drupal\file\Entity\File; /** @@ -40,8 +41,14 @@ public function testManagedFile(): void { $input_base_name = $tree ? 'nested_file' : 'file'; $file_field_name = $multiple ? 'files[' . $input_base_name . '][]' : 'files[' . $input_base_name . ']'; - // Submit without a file. $this->drupalGet($path); + + // Ensure the aria-describedby relationship works as expected. + $input_id = Html::getId('edit_' . $input_base_name); + $this->assertSession()->elementExists('css', '#' . $input_id . '--description'); + $this->assertSession()->elementExists('css', '[aria-describedby="' . $input_id . '--description"]'); + + // Submit without a file. $this->submitForm([], 'Save'); $this->assertSession()->pageTextContains("The file ids are .");