8000 [TwigBridge] bootstrap_3_layout ChoiceType's expanded label_html does not work · Issue #41189 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[TwigBridge] bootstrap_3_layout ChoiceType's expanded label_html does not work #41189
Closed
@jennevdmeer

Description

@jennevdmeer

Symfony version(s) affected: 5.1 not sure exactly but its this commit

{{- widget|raw }} {{ label is not same as(false) ? (translation_domain is same as(false) ? (label_html is same as(false) ? label : label|raw) : (label_html is same as(false) ? label|trans(label_translation_parameters, translation_domain) : label|trans(label_translation_parameters, translation_domain)|raw)) -}}

Description
In the nested ternary the |raw output is escaped non the less and thus still printed as plain text.

How to reproduce
Use bootstrap 3 form theme with ChoiceType expanded & label_html true and some html label.

Possible Solution
This has been fixed in the bootstrap 4 template https://github.com/symfony/symfony/blob/5.x/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig#L301-L318 I copied over the solution to a custom template locally and it works (note that bootstrap 3 has the widget in the label unlike in 4+):

{% block checkbox_radio_label -%}
    {# Do not display the label if widget is not defined in order to prevent double label rendering #}
    {%- if widget is defined -%}
        {%- if required -%}
            {%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' required')|trim}) -%}
        {%- endif -%}
        {%- if parent_label_class is defined -%}
            {%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' ' ~ parent_label_class)|trim}) -%}
        {%- endif -%}
        {%- if label is not same as(false) and label is empty -%}
            {%- if label_format is not empty -%}
                {%- set label = label_format|replace({
                    '%name%': name,
                    '%id%': id,
                }) -%}
            {%- else -%}
                {% set label = name|humanize %}
            {%- endif -%}
        {%- endif -%}
        <label{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}>
            {{- widget|raw }}

            <span>
                {%- if label is not same as(false) -%}
                    {%- if translation_domain is same as(false) -%}
                        {%- if label_html is same as(false) -%}
                            {{ label -}}
                        {%- else -%}
                            {{ label|raw -}}
                        {%- endif -%}
                    {%- else -%}
                        {%- if label_html is same as(false) -%}
                            {{ label|trans(label_translation_parameters, translation_domain) -}}
                        {%- else -%}
                            {{ label|trans(label_translation_parameters, translation_domain)|raw -}}
                        {%- endif -%}
                    {%- endif -%}
                {%- endif -%}
            </span>
        </label>
    {%- endif -%}
{%- endblock checkbox_radio_label %}

The span is optional, its something personal to help with css selectors as we can not select a text node.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0