From b962ed446db7e81f514b370f14f118f4403f463e Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Thu, 12 Jun 2014 18:20:10 +0100
Subject: [PATCH] Issue #2152215 by joelpittet, mr.baileys, benjifisher,
 martin107, rteijeiro, hussainweb, shanethehat, jenlampton, kpa, AnythonyR,
 EVIIILJ, kgoel, Cottser, dsdeiz, hanpersand, bayousoft: Convert
 theme_form_element_label() to Twig

---
 core/includes/form.inc                        | 33 ++++++++-----------
 core/includes/theme.inc                       |  1 +
 .../templates/form-element-label.html.twig    | 18 ++++++++++
 3 files changed, 32 insertions(+), 20 deletions(-)
 create mode 100644 core/modules/system/templates/form-element-label.html.twig

diff --git a/core/includes/form.inc b/core/includes/form.inc
index 5deb4939c06c..cc4bcdbb677a 100644
--- a/core/includes/form.inc
+++ b/core/includes/form.inc
@@ -2936,7 +2936,7 @@ function template_preprocess_form_element(&$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
@@ -2951,53 +2951,46 @@ function template_preprocess_form_element(&$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 '';
-  }
-
-  $title = Xss::filterAdmin($element['#title']);
-
-  $attributes = array();
+  $variables['title'] = (isset($element['#title']) && $element['#title'] !== '') ? Xss::filterAdmin($element['#title']) : '';
+  $variables['attributes'] = array();
   // Style the label as class option to display inline with the element.
   if ($element['#title_display'] == 'after') {
-    $attributes['class'][] = 'option';
+    $variables['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']['class'][] = 'visually-hidden';
   }
 
   // A #for property of a dedicated #type 'label' element as precedence.
   if (!empty($element['#for'])) {
-    $attributes['for'] = $element['#for'];
+    $variables['attributes']['for'] = $element['#for'];
     // A custom #id allows the referenced form input element to refer back to
     // the label element; e.g., in the 'aria-labelledby' attribute.
     if (!empty($element['#id'])) {
-      $attributes['id'] = $element['#id'];
+      $variables['attributes']['id'] = $element['#id'];
     }
   }
   // Otherwise, point to the #id of the form input element.
   elseif (!empty($element['#id'])) {
-    $attributes['for'] = $element['#id'];
+    $variables['attributes']['for'] = $element['#id'];
   }
 
   // For required elements a 'form-required' class is appended to the
   // label attributes.
+  $variables['required'] = FALSE;
   if (!empty($element['#required'])) {
-    $attributes['class'][] = 'form-required';
+    $variables['required'] = TRUE;
+    $variables['attributes']['class'][] = 'form-required';
   }
-
-  return '<label' . new Attribute($attributes) . '>' . $title . '</label>';
 }
 
 /**
diff --git a/core/includes/theme.inc b/core/includes/theme.inc
index fbcc619ab135..290d0e6001ec 100644
--- a/core/includes/theme.inc
+++ b/core/includes/theme.inc
@@ -2668,6 +2668,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 000000000000..37a0095b5a91
--- /dev/null
+++ b/core/modules/system/templates/form-element-label.html.twig
@@ -0,0 +1,18 @@
+{#
+/**
+ * @file
+ * Default theme implementation for a form element label.
+ *
+ * Available variables:
+ * - title: The label's text.
+ * - required: An indicator for whether the associated form element is required.
+ * - attributes: A list of HTML attributes for the label.
+ *
+ * @see template_preprocess_form_element_label()
+ *
+ * @ingroup themeable
+ */
+#}
+{% if title is not empty or required -%}
+  <label{{ attributes }}>{{ title }}</label>
+{%- endif %}
-- 
GitLab