diff --git a/core/includes/form.inc b/core/includes/form.inc index 3dc4e3c..9a9ae58 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -2710,13 +2710,16 @@ function template_preprocess_form_element(&$variables) { $variables['description'] = NULL; if (!empty($element['#description'])) { - $description_attributes = array('class' => 'description'); + $variables['description_display'] = $element['#description_display']; + $description_attributes = array('class' => array('description')); + if ($element['#description_display'] === 'invisible') { + $description_attributes['class'][] = 'visually-hidden'; + } if (!empty($element['#id'])) { $description_attributes['id'] = $element['#id'] . '--description'; } $variables['description']['attributes'] = new Attribute($description_attributes); $variables['description']['content'] = $element['#description']; - $variables['description_display'] = $element['#description_display']; } // Add label_display and label variables to template. diff --git a/core/modules/system/src/Tests/Form/ElementsLabelsTest.php b/core/modules/system/src/Tests/Form/ElementsLabelsTest.php index d015663..766386a 100644 --- a/core/modules/system/src/Tests/Form/ElementsLabelsTest.php +++ b/core/modules/system/src/Tests/Form/ElementsLabelsTest.php @@ -116,7 +116,7 @@ function testFormDescriptions() { // Check #description placement with #description_display='invisible'. $field_id = 'edit-form-textfield-test-description-invisible'; $description_id = $field_id . '--description'; - $elements = $this->xpath('//input[@id="' . $field_id . '" and @aria-describedby="' . $description_id . '"]/following-sibling::div[@class="visually-hidden"]'); + $elements = $this->xpath('//input[@id="' . $field_id . '" and @aria-describedby="' . $description_id . '"]/following-sibling::div[contains(@class, "visually-hidden")]'); $this->assertTrue(isset($elements[0]), t('Properly renders the #description element visually-hidden.')); } } diff --git a/core/modules/system/templates/form-element.html.twig b/core/modules/system/templates/form-element.html.twig index 053280a..bdd66a3 100644 --- a/core/modules/system/templates/form-element.html.twig +++ b/core/modules/system/templates/form-element.html.twig @@ -26,14 +26,16 @@ * label is not needed, such as a table of checkboxes where the row and * column provide the context. The tooltip will include the title and * required marker. - * - description_display: Description display setting. It can have these values: - * - before: The description is output before the element. - * - after: The description is output after the element. This is the default - * value. * - description: (optional) A list of description properties containing: * - content: A description of the form element, may not be set. * - attributes: (optional) A list of HTML attributes to apply to the * description content wrapper. Will only be set when description is set. + * - description_display: Description display setting. It can have these values: + * - before: The description is output before the element. + * - after: The description is output after the element. This is the default + * value. + * - invisible: The description is output after the element, hidden visually but + * available to screen readers. * * @see template_preprocess_form_element() * @@ -59,7 +61,7 @@ {% if label_display == 'after' %} {{ label }} {% endif %} - {% if description_display == 'after' and description.content %} + {% if description_display in ['after', 'invisible'] and description.content %}