diff --git a/core/includes/form.inc b/core/includes/form.inc index dd78e0d5040c0b52dd3b34d9a7baeaf41795ea1b..7dd168b5705cf9713410f5f3daf9fa1f5d4a1883 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -432,6 +432,7 @@ function template_preprocess_form_element(&$variables) { $element += array( '#title_display' => 'before', '#wrapper_attributes' => array(), + '#label_attributes' => array(), ); $variables['attributes'] = $element['#wrapper_attributes']; @@ -479,6 +480,7 @@ function template_preprocess_form_element(&$variables) { $variables['label_display'] = $element['#title_display']; $variables['label'] = array('#theme' => 'form_element_label'); $variables['label'] += array_intersect_key($element, array_flip(array('#id', '#required', '#title', '#title_display'))); + $variables['label']['#attributes'] = $element['#label_attributes']; $variables['children'] = $element['#children']; } diff --git a/core/modules/system/tests/src/Kernel/Form/FormElementLabelTest.php b/core/modules/system/tests/src/Kernel/Form/FormElementLabelTest.php index f93d461bcf580bcbb6597fd92455b11e233a2464..2bc895f8a0276190693c9bc8b1b846381b5e9872 100644 --- a/core/modules/system/tests/src/Kernel/Form/FormElementLabelTest.php +++ b/core/modules/system/tests/src/Kernel/Form/FormElementLabelTest.php @@ -31,6 +31,16 @@ public function testAttributes() { $this->render($render_array); $elements = $this->xpath($css_selector_converter->toXPath('.kitten')); $this->assertCount(1, $elements); + + // Add label attributes to a form element. + $render_array = [ + '#type' => 'textfield', + '#label_attributes' => ['class' => ['meow']], + '#title' => 'Kitten sounds', + ]; + $this->render($render_array); + $elements = $this->xpath($css_selector_converter->toXPath('label.meow')); + $this->assertCount(1, $elements); } }