C
C
Celsius2017-12-04 17:27:08
symfony
Celsius, 2017-12-04 17:27:08

How to override individual bootstrap3 twig templates for ChoiceType: 'radio' and 'select' in Symfony?

By default, the bootstrap_3_horizontal_layout.html.twig template adds the col-sm-2 class for the label and col-sm-10 for the form element itself, but I need to do the opposite with the Choice type: the col-sm-10 class for the label and col-sm -2 for the element, which I did by overriding the choice_row block with my own choice_row_choice and changing the choice_label block:

My twig template
{% block choice_row -%}
    {% set force_error = true %}
    {{- block('form_row_choice') }}
{%- endblock choice_row %}

{% block choice_label -%}
    {# remove the checkbox-inline and radio-inline class, it's only useful for embed labels #}
    {#{%- set label_attr = label_attr|merge({class: label_attr.class|default('')|replace({'checkbox-inline': '', 'radio-inline': ''})|trim}) -%}#}
    {#{{- block('form_label_choice') -}}#}
    {% spaceless %}
        {% if label is same as(false) %}
            <div class="{{ block('form_label_class_choice') }}"></div>
        {% else %}
            {% set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' ' ~ block('form_label_class_choice'))|trim}) %}
            {{ block('form_label_choice') }}
        {% endif %}
    {% endspaceless %}
{% endblock %}

{% block form_row_choice -%}
    <div id="_{{ id }}" class="form-group{% if (not compound or force_error|default(false)) and not valid %} has-error{% endif %}">
        {{- form_label(form) -}}
        <div class="{{ block('form_group_class_choice') }}">
            {{- form_widget(form) -}}
            {{- form_errors(form) -}}
        </div>
    </div>
{%- endblock form_row_choice %}

{% block form_label_class_choice -%}
    col-sm-10
{%- endblock form_label_class_choice %}

{% block form_group_class_choice -%}
    col-sm-2
{%- endblock form_group_class_choice %}

{%- block form_label_choice -%}
    {% if label is not same as(false) -%}
        {% if not compound -%}
            {% set label_attr = label_attr|merge({'for': id}) %}
        {%- endif -%}
        {% if required -%}
            {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
        {%- endif -%}
        {% if 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{% if label_attr %}{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}{% endif %}>{{ translation_domain is same as(false) ? label : label|trans({}, translation_domain) }}</label>
    {%- endif -%}
{%- endblock form_label_choice -%}


I have my own ChoiceType - with radio buttons, EntityType - a regular select, although this is probably not important.
The problem is that fields with EntityType also use this template, and I need EntityType to have its own template.
Pattern output sequence:

AAAAAAAAAAAA - мой шаблон
5a2558ec96ca2212436828.png

How to display choice_label for Choice type, and for Entity, for example, display your own entity_label?
I figured it out on my own: let's
add the condition '{% if expanded %}' and accordingly display different blocks further:
{% block choice_row -%}
    {% set force_error = true %}
    {% if expanded %}
        {{- block('form_row_choice') }}
    {% else %}
        {{- block('form_row_choice_collaps') }}
    {% endif %}
{%- endblock choice_row %}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question