Skip to content

Commit 5015ba8

Browse files
committed
Fix required checkboxes and radios to show required markers when required
1 parent b38c730 commit 5015ba8

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{#
2+
/**
3+
* @file
4+
* Default theme implementation for a fieldset element and its children.
5+
*
6+
* Available variables:
7+
* - attributes: HTML attributes for the <fieldset> element.
8+
* - errors: (optional) Any errors for this <fieldset> element, may not be set.
9+
* - required: Boolean indicating whether the <fieldset> element is required.
10+
* - legend: The <legend> element containing the following properties:
11+
* - title: Title of the <fieldset>, intended for use as the text
12+
of the <legend>.
13+
* - attributes: HTML attributes to apply to the <legend> element.
14+
* - description: The description element containing the following properties:
15+
* - content: The description content of the <fieldset>.
16+
* - attributes: HTML attributes to apply to the description container.
17+
* - description_display: Description display setting. It can have these values:
18+
* - before: The description is output before the element.
19+
* - after: The description is output after the element (default).
20+
* - invisible: The description is output after the element, hidden visually
21+
* but available to screen readers.
22+
* - children: The rendered child elements of the <fieldset>.
23+
* - prefix: The content to add before the <fieldset> children.
24+
* - suffix: The content to add after the <fieldset> children.
25+
*
26+
* @see template_preprocess_fieldset()
27+
*
28+
* @ingroup themeable
29+
*/
30+
#}
31+
{%
32+
set classes = [
33+
'js-form-item',
34+
'form-item',
35+
'js-form-wrapper',
36+
'form-wrapper',
37+
]
38+
%}
39+
<fieldset{{ attributes.addClass(classes) }}>
40+
{%
41+
set legend_span_classes = [
42+
'fieldset-legend',
43+
required ? 'js-form-required',
44+
required ? 'form-required',
45+
]
46+
%}
47+
{%
48+
set legend_classes = [
49+
required ? 'required',
50+
]
51+
%}
52+
{# Always wrap fieldset legends in a <span> for CSS positioning. #}
53+
<legend{{ legend.attributes.addClass(legend_classes) }}>
54+
<span{{ legend_span.attributes.addClass(legend_span_classes) }}>{{ legend.title }}</span>
55+
{% if required %}
56+
<strong class="required">({{ 'required'|t }})</strong>
57+
{% endif %}
58+
</legend>
59+
<div class="fieldset-wrapper">
60+
{% if description_display == 'before' and description.content %}
61+
<div{{ description.attributes.addClass('description') }}>{{ description.content }}</div>
62+
{% endif %}
63+
{% if errors %}
64+
<div>
65+
{{ errors }}
66+
</div>
67+
{% endif %}
68+
{% if prefix %}
69+
<span class="field-prefix">{{ prefix }}</span>
70+
{% endif %}
71+
{{ children }}
72+
{% if suffix %}
73+
<span class="field-suffix">{{ suffix }}</span>
74+
{% endif %}
75+
{% if description_display in ['after', 'invisible'] and description.content %}
76+
<div{{ description.attributes.addClass('description') }}>{{ description.content }}</div>
77+
{% endif %}
78+
</div>
79+
</fieldset>

wxt_bootstrap.theme

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,21 @@ function wxt_bootstrap_preprocess_form_element(array &$variables) {
347347
$variables['required'] = TRUE;
348348
}
349349
}
350+
351+
/**
352+
* Implements hook_preprocess_fieldset().
353+
*/
354+
function wxt_bootstrap_preprocess_fieldset(array &$variables) {
355+
$element = $variables['element'] ?? [];
356+
$checkbox_radio_group = [
357+
'radios',
358+
'checkboxes',
359+
];
360+
361+
if (in_array($element['#type'], $checkbox_radio_group)) {
362+
// Fieldset wrapper uses {{ attributes }} in Twig, so add the class here.
363+
if (isset($variables['attributes'])) {
364+
$variables['attributes']['class'][] = 'chkbxrdio-grp';
365+
}
366+
}
367+
}

0 commit comments

Comments
 (0)