diff --git a/core/includes/form.inc b/core/includes/form.inc index efb59f7..c948202 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -1062,31 +1062,20 @@ function template_preprocess_fieldset(&$variables) { _form_set_attributes($element, array('form-wrapper')); $variables['attributes'] = $element['#attributes']; $variables['attributes']['class'][] = 'form-item'; - - // If the element is required, a required marker is appended to the label. - $variables['required'] = ''; - if (!empty($element['#required'])) { - $variables['required'] = array( - '#theme' => 'form_required_marker', - '#element' => $element, - ); - } - $variables['prefix'] = isset($element['#field_prefix']) ? $element['#field_prefix'] : NULL; $variables['suffix'] = isset($element['#field_suffix']) ? $element['#field_suffix'] : NULL; $variables['children'] = $element['#children']; - - // Build legend properties. - $variables['legend'] = array(); $legend_attributes = array(); if (isset($element['#title_display']) && $element['#title_display'] == 'invisible') { $legend_attributes['class'][] = 'visually-hidden'; } $variables['legend']['attributes'] = new Attribute($legend_attributes); $variables['legend']['title'] = (isset($element['#title']) && $element['#title'] !== '') ? Xss::filterAdmin($element['#title']) : ''; - - // Build description properties. - $variables['description'] = array(); + $legend_span_attributes = array('class' => array('fieldset-legend')); + if (!empty($element['#required'])) { + $legend_span_attributes['class'][] = 'form-required'; + $variables['legend_span']['attributes'] = new Attribute($legend_span_attributes); + } if (!empty($element['#description'])) { $description_id = $element['#attributes']['id'] . '--description'; $description_attributes = array( @@ -2926,7 +2915,7 @@ function template_preprocess_form_element(&$variables) { $variables['attributes']['class'][] = 'form-disabled'; } - // If #title is not set, we don't display any label or required marker. + // If #title is not set, we don't display any label. if (!isset($element['#title'])) { $element['#title_display'] = 'none'; } @@ -2952,23 +2941,6 @@ function template_preprocess_form_element(&$variables) { } /** - * Returns HTML for a marker for required form elements. - * - * @param $variables - * An associative array containing: - * - element: An associative array containing the properties of the element. - * - * @ingroup themeable - */ -function theme_form_required_marker($variables) { - $attributes = array( - 'class' => 'form-required', - 'aria-hidden' => 'true', - ); - return '*'; -} - -/** * Returns HTML for a form element label and required marker. * * Form element labels include the #title and a #required marker. The label is @@ -2998,26 +2970,16 @@ function theme_form_element_label($variables) { return ''; } - // If the element is required, a required marker is appended to the label. - $required = ''; - if (!empty($element['#required'])) { - $marker = array( - '#theme' => 'form_required_marker', - '#element' => $element, - ); - $required = drupal_render($marker); - } - $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'; + $attributes['class'][] = 'option'; } // Show label only to screen readers to avoid disruption in visual flows. elseif ($element['#title_display'] == 'invisible') { - $attributes['class'] = 'visually-hidden'; + $attributes['class'][] = 'visually-hidden'; } // A #for property of a dedicated #type 'label' element as precedence. @@ -3034,7 +2996,13 @@ function theme_form_element_label($variables) { $attributes['for'] = $element['#id']; } - return ''; + // For required elements a 'form-required' class is appended to the + // label attributes. + if (!empty($element['#required'])) { + $attributes['class'][] = 'form-required'; + } + + return ''; } /** diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 654803c..9b5d9e9 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -2662,9 +2662,6 @@ function drupal_common_theme() { 'render element' => 'element', 'template' => 'form-element', ), - 'form_required_marker' => array( - 'render element' => 'element', - ), 'form_element_label' => array( 'render element' => 'element', ), diff --git a/core/misc/states.js b/core/misc/states.js index 701d6fc..530f03e 100644 --- a/core/misc/states.js +++ b/core/misc/states.js @@ -522,12 +522,12 @@ if (e.value) { var $label = $(e.target).attr({ 'required': 'required', 'aria-required': 'aria-required' }).closest('.form-item, .form-wrapper').find('label'); // Avoids duplicate required markers on initialization. - if (!$label.find('.form-required').length) { - $label.append(Drupal.theme('requiredMarker')); + if (!$label.hasClass('form-required').length) { + $label.addClass('form-required'); } } else { - $(e.target).removeAttr('required aria-required').closest('.form-item, .form-wrapper').find('label .form-required').remove(); + $(e.target).removeAttr('required aria-required').closest('.form-item, .form-wrapper').find('label.form-required').removeClass('form-required'); } } }); @@ -573,10 +573,4 @@ return (a === b) ? (typeof a === 'undefined' ? a : true) : (typeof a === 'undefined' || typeof b === 'undefined'); } - $.extend(Drupal.theme, { - requiredMarker: function () { - return '*'; - } - }); - })(jQuery); diff --git a/core/modules/datetime/datetime.module b/core/modules/datetime/datetime.module index c8dd4d1..3d13b7b 100644 --- a/core/modules/datetime/datetime.module +++ b/core/modules/datetime/datetime.module @@ -226,13 +226,6 @@ function template_preprocess_datetime_form(&$variables) { function template_preprocess_datetime_wrapper(&$variables) { $element = $variables['element']; - // If the element is required, a required marker is appended to the label. - $form_required_marker = array( - '#theme' => 'form_required_marker', - '#element' => $element, - ); - $variables['required'] = !empty($element['#required']) ? drupal_render($form_required_marker) : ''; - if (!empty($element['#title'])) { $variables['title'] = $element['#title']; } @@ -241,6 +234,13 @@ function template_preprocess_datetime_wrapper(&$variables) { $variables['description'] = $element['#description']; } + $title_attributes = array('class' => array('label')); + // For required datetime fields a 'form-required' class is appended to the + // label attributes. + if (!empty($element['#required'])) { + $title_attributes['class'][] = 'form-required'; + } + $variables['title_attributes'] = new Attribute($title_attributes); $variables['content'] = $element['#children']; } diff --git a/core/modules/datetime/lib/Drupal/datetime/Tests/DateTimeFieldTest.php b/core/modules/datetime/lib/Drupal/datetime/Tests/DateTimeFieldTest.php index 0302c72..d8c976a 100644 --- a/core/modules/datetime/lib/Drupal/datetime/Tests/DateTimeFieldTest.php +++ b/core/modules/datetime/lib/Drupal/datetime/Tests/DateTimeFieldTest.php @@ -98,7 +98,7 @@ function testDateField() { // Display creation form. $this->drupalGet('entity_test/add'); $this->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.'); - $this->assertFieldByXPath('//*[@id="edit-' . $field_name . '-wrapper"]/h4/span', '*', 'Required markup found'); + $this->assertFieldByXPath('//*[@id="edit-' . $field_name . '-wrapper"]/h4[contains(@class, "form-required")]', TRUE, 'Required markup found'); $this->assertNoFieldByName("{$field_name}[0][value][time]", '', 'Time element not found.'); // Submit a valid date and ensure it is accepted. diff --git a/core/modules/datetime/templates/datetime-wrapper.html.twig b/core/modules/datetime/templates/datetime-wrapper.html.twig index a4acf70..5660cf5 100644 --- a/core/modules/datetime/templates/datetime-wrapper.html.twig +++ b/core/modules/datetime/templates/datetime-wrapper.html.twig @@ -6,8 +6,7 @@ * Available variables: * - content: The form element to be output, usually a datelist, or datetime. * - title: The title of the form element. - * - attributes: HTML attributes for the form wrapper. - * - required: (optional) A marker indicating that the form element is required. + * - title_attributes: HTML attributes for the title wrapper. * - description: Description text for the form element. * * @see template_preprocess_datetime_wrapper() @@ -16,9 +15,7 @@ */ #} {% if title %} -