From dd77893f4d9286981c408409b61a839291a39aa6 Mon Sep 17 00:00:00 2001 From: webchick <webchick@24967.no-reply.drupal.org> Date: Fri, 7 Mar 2014 14:26:17 -0800 Subject: [PATCH] Issue #2152209 by longwave, joelpittet, steveoliver, hussainweb, shanethehat, jenlampton, kpa, AnythonyR, EVIIILJ, kgoel, Cottser, dsdeiz, hanpersand: Convert theme_fieldset() to Twig. --- core/includes/form.inc | 72 +++++++++---------- core/includes/theme.inc | 1 + .../system/templates/fieldset.html.twig | 42 +++++++++++ 3 files changed, 75 insertions(+), 40 deletions(-) create mode 100644 core/modules/system/templates/fieldset.html.twig diff --git a/core/includes/form.inc b/core/includes/form.inc index 886745683ce8..710b84822ae6 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -9,6 +9,7 @@ use Drupal\Component\Utility\Number; use Drupal\Component\Utility\String; use Drupal\Component\Utility\UrlHelper; +use Drupal\Component\Utility\Xss; use Drupal\Core\Database\Database; use Drupal\Core\Language\Language; use Drupal\Core\Template\Attribute; @@ -1001,68 +1002,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 = '<fieldset' . new Attribute($element['#attributes']) . '>'; - - if ((isset($element['#title']) && $element['#title'] !== '') || !empty($element['#required'])) { - // Always wrap fieldset legends in a SPAN for CSS positioning. - $output .= '<legend' . new Attribute($legend_attributes) . '><span class="fieldset-legend">'; - $output .= t('!title!required', array('!title' => $element['#title'], '!required' => $required)); - $output .= '</span></legend>'; - } - $output .= '<div class="fieldset-wrapper">'; - - if (isset($element['#field_prefix'])) { - $output .= '<span class="field-prefix">' . $element['#field_prefix'] . '</span> '; - } - $output .= $element['#children']; - if (isset($element['#field_suffix'])) { - $output .= ' <span class="field-suffix">' . $element['#field_suffix'] . '</span>'; - } + // Build description properties. + $variables['description'] = array(); if (!empty($element['#description'])) { - $attributes = array('class' => 'description', 'id' => $description_id); - $output .= '<div' . new Attribute($attributes) . '>' . $element['#description'] . '</div>'; + $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 .= '</div>'; - $output .= "</fieldset>\n"; - return $output; } /** diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 0787de70f8bd..5a6b10948e3d 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -2592,6 +2592,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 000000000000..9e4fe68c534e --- /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 + */ +#} +<fieldset{{ attributes }}> + {% if legend.title is not empty or required -%} + {# Always wrap fieldset legends in a SPAN for CSS positioning. #} + <legend{{ legend.attributes }}><span class="fieldset-legend">{{ legend.title }}{{ required }}</span></legend> + {%- endif %} + <div class="fieldset-wrapper"> + {% if prefix %} + <span class="field-prefix">{{ prefix }}</span> + {% endif %} + {{ children }} + {% if suffix %} + <span class="field-suffix">{{ suffix }}</span> + {% endif %} + {% if description.content %} + <div{{ description.attributes }}>{{ description.content }}</div> + {% endif %} + </div> +</fieldset> -- GitLab