diff --git a/core/includes/form.inc b/core/includes/form.inc
index 87f8edf..03824f8 100644
--- a/core/includes/form.inc
+++ b/core/includes/form.inc
@@ -8,6 +8,7 @@
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\Number;
use Drupal\Component\Utility\String;
+use Drupal\Component\Utility\Xss;
use Drupal\Component\Utility\Url;
use Drupal\Core\Database\Database;
use Drupal\Core\Language\Language;
@@ -2820,7 +2821,7 @@ function theme_form_required_marker($variables) {
}
/**
- * Returns HTML for a form element label and required marker.
+ * Prepares variables for form label templates.
*
* Form element labels include the #title and a #required marker. The label is
* associated with the element itself by the element #id. Labels may appear
@@ -2835,41 +2836,39 @@ function theme_form_required_marker($variables) {
* required. That is especially important for screenreader users to know
* which field is required.
*
- * @param $variables
+ * @param array $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #required, #title, #id, #value, #description.
*
- * @ingroup themeable
*/
-function theme_form_element_label($variables) {
+function template_preprocess_form_element_label(&$variables) {
$element = $variables['element'];
// If title and required marker are both empty, output no label.
if ((!isset($element['#title']) || $element['#title'] === '') && empty($element['#required'])) {
- return '';
+ $variables['value'] = '';;
}
+ else {
+ // If the element is required, a required marker is appended to the label.
+ $variables['required'] = !empty($element['#required']) ? theme('form_required_marker', array('element' => $element)) : '';
- // If the element is required, a required marker is appended to the label.
- $required = !empty($element['#required']) ? theme('form_required_marker', array('element' => $element)) : '';
-
- $title = filter_xss_admin($element['#title']);
+ $variables['title'] = Xss::filterAdmin($element['#title']);
- $attributes = array();
- // Style the label as class option to display inline with the element.
- if ($element['#title_display'] == 'after') {
- $attributes['class'] = 'option';
- }
- // Show label only to screen readers to avoid disruption in visual flows.
- elseif ($element['#title_display'] == 'invisible') {
- $attributes['class'] = 'visually-hidden';
- }
+ $variables['attributes'] = array();
+ // Style the label as class option to display inline with the element.
+ if ($element['#title_display'] == 'after') {
+ $variables['attributes']['class'][] = 'option';
+ }
+ // Show label only to screen readers to avoid disruption in visual flows.
+ elseif ($element['#title_display'] == 'invisible') {
+ $variables['attributes']['class'][] = 'visually-hidden';
+ }
- if (!empty($element['#id'])) {
- $attributes['for'] = $element['#id'];
+ if (!empty($element['#id'])) {
+ $variables['attributes']['for'] = $element['#id'];
+ }
}
-
- return '';
}
/**
diff --git a/core/includes/theme.inc b/core/includes/theme.inc
index dd50e62..68b46f5 100644
--- a/core/includes/theme.inc
+++ b/core/includes/theme.inc
@@ -2697,6 +2697,7 @@ function drupal_common_theme() {
),
'form_element_label' => array(
'render element' => 'element',
+ 'template' => 'form-element-label',
),
'vertical_tabs' => array(
'render element' => 'element',
diff --git a/core/modules/system/templates/form-element-label.html.twig b/core/modules/system/templates/form-element-label.html.twig
new file mode 100644
index 0000000..f732943
--- /dev/null
+++ b/core/modules/system/templates/form-element-label.html.twig
@@ -0,0 +1,12 @@
+{#
+/**
+ * @file
+ * Default theme implementation for a form element label.
+ *
+ * Available variables:
+ * - attributes: A list of HTML attributes for the label.
+ *
+ * @ingroup themeable
+ */
+#}
+