summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwebchick2014-03-07 22:26:17 (GMT)
committerwebchick2014-03-07 22:26:17 (GMT)
commitdd77893f4d9286981c408409b61a839291a39aa6 (patch)
tree5e7aa36f1819a59f447b6774382dcb7f6a08b445
parentad5980c7aee3085552ec179f6f2513ad43c8933f (diff)
Issue #2152209 by longwave, joelpittet, steveoliver, hussainweb, shanethehat, jenlampton, kpa, AnythonyR, EVIIILJ, kgoel, Cottser, dsdeiz, hanpersand: Convert theme_fieldset() to Twig.
-rw-r--r--core/includes/form.inc72
-rw-r--r--core/includes/theme.inc1
-rw-r--r--core/modules/system/templates/fieldset.html.twig42
3 files changed, 75 insertions, 40 deletions
diff --git a/core/includes/form.inc b/core/includes/form.inc
index 8867456..710b848 100644
--- a/core/includes/form.inc
+++ b/core/includes/form.inc
@@ -9,6 +9,7 @@ use Drupal\Component\Utility\NestedArray;
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 0787de7..5a6b109 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 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
+ */
+#}
+<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>