Skip to content

Fix/337 optional label radio checkbox#344

Open
marek7gol wants to merge 2 commits intolocalgovdrupal:2.xfrom
marek7gol:fix/337-optional-label-radio-checkbox
Open

Fix/337 optional label radio checkbox#344
marek7gol wants to merge 2 commits intolocalgovdrupal:2.xfrom
marek7gol:fix/337-optional-label-radio-checkbox

Conversation

@marek7gol
Copy link

Summary

Fixes #337 - Webform item labelling not working for radio/checkbox groups.

This PR replaces the CSS-based approach for displaying "(optional)" labels with PHP preprocess functions, which provide more reliable and consistent behaviour across all form element types.

Problem

The existing CSS-based solution using --form-item-optional-pseudocontent had several issues:

  1. Radio/checkbox groups: The "(optional)" text was incorrectly displayed on individual radio/checkbox control labels when selected, rather than on the fieldset legend.
  2. Required parent elements: When a radio or checkbox was part of a required fieldset (using Webform's "Radios" or "Checkboxes" parent elements), the individual control labels still incorrectly displayed "(optional)" when selected.
  3. Translation: The CSS-based approach using pseudo-content cannot be translated for multilingual sites (Irish, Welsh, etc.).

Solution

This PR implements two PHP preprocess functions that handle the "(optional)" label logic:

  1. localgov_microsites_base_preprocess_fieldset()

Appends "(optional)" to the fieldset legend for supported element types: radios, checkboxes, webform_uk_address, webform_address. Only applies when the fieldset (parent element) is not required and has a legend title.

  1. localgov_microsites_base_preprocess_form_element()

Appends "(optional)" to standalone (not being a part of a fieldset/parent element) form element labels. Supported types: checkbox, radio, textfield, textarea, email, tel, number, url, date, datetime, select. Detects if a radio/checkbox is part of a fieldset/parent element (by checking #array_parents depth) and skips individual labels in that case, as the legend already displays the "(optional)" text.

Changes

  1. Added two preprocess functions to localgov_microsites_base.theme file.
  2. Removed --form-item-required-pseudocontent and --form-item-optional-pseudocontent CSS variables from css/base/variables.css file.
  3. Removed references to these CSS variables from css/components/forms.css file.

Remove the --form-item-required-pseudocontent and --form-item-optional-pseudocontent variables from variables.css file and their references from the forms.css file.
@marek7gol
Copy link
Author

Hi @danchamp, @markconroy, @msayoung 👋
This PR is ready for review. It addresses the issues raised in #337 regarding the "(optional)" labelling for radio/checkbox groups. I'd appreciate your feedback when you have a moment. Thanks!

@dedavidson dedavidson added the bug Something isn't working label Nov 27, 2025
@dedavidson
Copy link
Contributor

@markconroy would you able to take a look at this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Webform item labelling not working for radio/checkbox groups

2 participants