Issue #2998857 by Luke.Leber, e.chatrer, afem, bskibinski, mgifford: a11y: Input type file fields lack aria-describedby to the description

merge-requests/9016/head
Alex Pott 2024-07-10 08:53:46 +01:00
parent e015f80827
commit 74b6cedac2
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
3 changed files with 14 additions and 1 deletions

View File

@ -320,6 +320,11 @@ class ManagedFile extends FormElementBase {
'#weight' => -10, '#weight' => -10,
'#error_no_message' => TRUE, '#error_no_message' => TRUE,
]; ];
if (!empty($element['#description'])) {
$element['upload']['#attributes']['aria-describedby'] = $element['#id'] . '--description';
}
if (!empty($element['#accept'])) { if (!empty($element['#accept'])) {
$element['upload']['#attributes'] = ['accept' => $element['#accept']]; $element['upload']['#attributes'] = ['accept' => $element['#accept']];
} }

View File

@ -41,6 +41,7 @@ class FileModuleTestForm extends FormBase {
$form['nested']['file'] = [ $form['nested']['file'] = [
'#type' => 'managed_file', '#type' => 'managed_file',
'#title' => $this->t('Managed <em>@type</em>', ['@type' => 'file & butter']), '#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', '#upload_location' => 'public://test',
'#progress_message' => $this->t('Processing...'), '#progress_message' => $this->t('Processing...'),
'#extended' => (bool) $extended, '#extended' => (bool) $extended,

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Drupal\Tests\file\Functional; namespace Drupal\Tests\file\Functional;
use Drupal\Component\Utility\Html;
use Drupal\file\Entity\File; use Drupal\file\Entity\File;
/** /**
@ -40,8 +41,14 @@ class FileManagedFileElementTest extends FileFieldTestBase {
$input_base_name = $tree ? 'nested_file' : 'file'; $input_base_name = $tree ? 'nested_file' : 'file';
$file_field_name = $multiple ? 'files[' . $input_base_name . '][]' : 'files[' . $input_base_name . ']'; $file_field_name = $multiple ? 'files[' . $input_base_name . '][]' : 'files[' . $input_base_name . ']';
// Submit without a file.
$this->drupalGet($path); $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->submitForm([], 'Save');
$this->assertSession()->pageTextContains("The file ids are ."); $this->assertSession()->pageTextContains("The file ids are .");