Unable to theme Checkbox and Radio Buttons

Created on 22 November 2023, 7 months ago

I am pretty sure there is an issue with bootstrap barrio's implementation of checkboxes and radio buttons. I struggled with this for a while so thought I would share the solution.

Firstly, it should be possible to add a class to a label.

e.g.

    // '#label_attributes' => ['class' => ['btn', 'btn-primary']],

However this does not seem to be possible with your implementation where you have the following in form-element.html.twig:

    {% if label_display == 'after' %}
      <label {{ label_attributes.addClass(labelclass).setAttribute('for', input_attributes.id) }}>
        {{ input_title | raw }}
      </label>
    {% endif %}

My solution to this was to replace with the following:

    {% if label_display == 'after' %}
      {{ label }}
    {% endif %}

This uses the label as defined in templates/form/form-element-label.html.twig e.g.

{% if title is not empty or required -%}
  <label{{ attributes.addClass(classes) }}>{{ title }}</label>
{%- endif %}

This allows you to set classes etc on the labels.

πŸ’¬ Support request
Status

Closed: works as designed

Version

5.5

Component

Code

Created by

πŸ‡¬πŸ‡§United Kingdom 2dareis2do

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024