Да, но вы должны перезаписать блок choice_widget_expanded.
ArticleType:
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use App\Entity\Category;
class ArticleType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('category', null, [
'expanded' => true,
'multiple' => true,
// make groups with the CategoryType entity
'group_by' => function(Category $category, $key, $value) {
return $category->getType()->getName();
},
// sort the categoryTypes alpabetical
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('c')
->join('c.type', 't')
->orderBy('t.name', 'ASC');
},
])
;
}
}
extended_form_layout.html.twig:
{% block choice_widget_expanded -%}
{% if '-inline' in label_attr.class|default('') -%}
<div class="control-group">
{%- for child in form %}
{{- form_widget(child, {
parent_label_class: label_attr.class|default(''),
translation_domain: choice_translation_domain,
}) -}}
{% endfor -%}
</div>
{%- else -%}
<div {{ block('widget_container_attributes') }}>
<div class="row">
{% for group_label, choice in choices %}
<div class="col-lg-3 col-md-4 col-sm-4 col-xs-6">
<h4>{{ group_label }}</h4>
{% for choice in choice.choices %}
{{- form_widget(form[choice.value], {
parent_label_class: label_attr.class|default(''),
translation_domain: choice_translation_domain,
}) -}}
{% endfor -%}
</div>
{% if not(loop.index % 3) %}</div><div class="row">{% endif %}
{% endfor -%}
</div>
</div>
{%- endif %}
{%- endblock choice_widget_expanded %}
Обновление: Я получил следующий виджет для Symfony ~4.3:
{%- block choice_widget_expanded -%}
<div {{ block('widget_container_attributes') }}>
{% for group_label, choice in choices %}
<h4>{{ choice_translation_domain is same as(false) ? group_label : group_label|trans({}, choice_translation_domain) }}</h4>
{% for choice in choice.choices %}
{{- form_widget(form[choice.value]) -}}
{{- form_label(form[choice.value], null, {translation_domain: choice_translation_domain}) -}}
{% endfor %}
{% endfor %}
</div>
{%- endblock choice_widget_expanded -%}
edit.html.twig:
{% extends 'base.html.twig' %}
{% form_theme edit_form 'extended_form_layout.html.twig' %}
{{ form(edit_form) }}