diff --git a/core/includes/form.inc b/core/includes/form.inc index 62e8f9a..03ea704 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; @@ -1000,68 +1001,59 @@ function form_get_options($element, $key) { } /** - * Returns HTML for a fieldset form element and its children. + * Prepares variables for fieldset element templates. * - * @param $variables + * Default template: fieldset.html.twig. + * + * @param array $variables * An associative array containing: * - element: An associative array containing the properties of the element. - * Properties used: #attributes, #children, #description, #id, - * #title, #value. - * - * @ingroup themeable + * Properties used: #attributes, #children, #description, #id, #title, + * #value. */ -function theme_fieldset($variables) { +function template_preprocess_fieldset(&$variables) { $element = $variables['element']; element_set_attributes($element, array('id')); _form_set_attributes($element, array('form-wrapper')); - - $element['#attributes']['class'][] = 'form-item'; - - if (!empty($element['#description'])) { - $description_id = $element['#attributes']['id'] . '--description'; - $element['#attributes']['aria-describedby'] = $description_id; - } + $variables['attributes'] = $element['#attributes']; + $variables['attributes']['class'][] = 'form-item'; // If the element is required, a required marker is appended to the label. - // @see theme_form_element_label() - $required = ''; + $variables['required'] = ''; if (!empty($element['#required'])) { - $marker = array( + $variables['required'] = array( '#theme' => 'form_required_marker', '#element' => $element, ); - $required = drupal_render($marker); } + $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']) : ''; - $output = ''; - - if ((isset($element['#title']) && $element['#title'] !== '') || !empty($element['#required'])) { - // Always wrap fieldset legends in a SPAN for CSS positioning. - $output .= ''; - $output .= t('!title!required', array('!title' => $element['#title'], '!required' => $required)); - $output .= ''; - } - $output .= '
'; - - if (isset($element['#field_prefix'])) { - $output .= '' . $element['#field_prefix'] . ' '; - } - $output .= $element['#children']; - if (isset($element['#field_suffix'])) { - $output .= ' ' . $element['#field_suffix'] . ''; - } + // Build description properties. + $variables['description'] = array(); if (!empty($element['#description'])) { - $attributes = array('class' => 'description', 'id' => $description_id); - $output .= '' . $element['#description'] . '
'; + $description_id = $element['#attributes']['id'] . '--description'; + $description_attributes = array( + 'class' => 'description', + 'id' => $description_id, + ); + $variables['description']['attributes'] = new Attribute($description_attributes); + $variables['description']['content'] = $element['#description']; + + // Add the description's id to the fieldset aria attributes. + $variables['attributes']['aria-describedby'] = $description_id; } - $output .= ''; - $output .= "\n"; - return $output; } /** diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 7ad60e5..35405ec 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -2659,6 +2659,7 @@ function drupal_common_theme() { ), 'fieldset' => array( 'render element' => 'element', + 'template' => 'fieldset', ), 'details' => array( 'render element' => 'element', diff --git a/core/modules/system/templates/fieldset.html.twig b/core/modules/system/templates/fieldset.html.twig new file mode 100644 index 0000000..9e4fe68 --- /dev/null +++ b/core/modules/system/templates/fieldset.html.twig @@ -0,0 +1,42 @@ +{# +/** + * @file + * Default theme implementation for a fieldset element and its children. + * + * Available variables: + * - attributes: HTML attributes for the fieldset element. + * - required: The required marker or empty if the associated fieldset is + * not required. + * - legend: The legend element containing the following properties: + * - title: Title of the fieldset, intended for use as the text of the legend. + * - attributes: HTML attributes to apply to the legend. + * - description: The description element containing the following properties: + * - content: The description content of the fieldset. + * - attributes: HTML attributes to apply to the description container. + * - children: The rendered child elements of the fieldset. + * - prefix: The content to add before the fieldset children. + * - suffix: The content to add after the fieldset children. + * + * @see template_preprocess_fieldset() + * + * @ingroup themeable + */ +#} + + {% if legend.title is not empty or required -%} + {# Always wrap fieldset legends in a SPAN for CSS positioning. #} + {{ legend.title }}{{ required }} + {%- endif %} +
+ {% if prefix %} + {{ prefix }} + {% endif %} + {{ children }} + {% if suffix %} + {{ suffix }} + {% endif %} + {% if description.content %} + {{ description.content }}
+ {% endif %} + +