Symfony Twig sets up a custom collection field

There is a way in the documentation in Symfony to customize an individual field based on the widget name / ID.

{% form_theme form _self %} {% block _product_name_widget %} <div class="text_widget"> {{ block('field_widget') }} </div> {% endblock %} {{ form_widget(form.name) }} 

Here, the _product_name_widget fragment defines the template that will be used for the field whose id is product_name (and name is product [name]).

This works for a regular widget, but not for a widget inside a collection. Due to extra columns. Like this:

 name="productbundle_product_type[foobar][1][value]" id="productbundle_product_type_foobar_1_value" 

How can I tweak the twig setting inside the collection?

I thought something like this, but this does not work:

 {% for db in edit_form.list %} {% block _productbundle_product_type_foobar_{{ db.name }}_widget %} <div class="text_widget"> {{ block('field_widget') }} </div> {% endblock %} {% endfor %} 

Even the following does not work:

 {% _productbundle_product_type_foobar_1_value_widget %} 

What is the way to make it work?

+8
source share
2 answers

I worked on the project a couple of days ago and faced exactly this problem - the solution I found is a couple of blocks that look like this (lacking project-specific code):

 {# Collection markup #} {% block my_collection_widget %} {# Customise collection row prototype for allow_add #} {% if prototype is defined %} {% set data_prototype = block('my_collection_item_widget') %} <div id="my_prototype" data-prototype="{{ data_prototype }}" style="display: none"></div> {% endif %} {% for prototype in form %} {{ block('my_collection_item_widget') }} {% endfor %} {% endblock my_collection_widget %} {# Collection row markup #} {% block my_collection_item_widget %} {# Collection contains simple, single type #} {{ form_errors(prototype) }} {{ form_label(prototype) }} {{ form_widget(prototype) }} {# OR #} {# Collection contains a compound type, render child types independantly #} {{ form_errors(prototype.inner_widget) }} {{ form_label(prototype.inner_widget) }} {{ form_widget(prototype.inner_widget) }} {% endblock my_collection_item_widget %} 
+3
source

I know this is an old question, but maybe people are still faced with this. This is due to the name of the fragments for the collections .

In these cases, you use _entry_ instead of the collection item number. Use the instructions in the link to name the fragments, but this may differ. Sometimes “type” is part of the fragment name, sometimes the first letter is uppercase, sometimes lowercase, etc. I would use a browser developer tool to find the real name to make sure. You can also customize the names used by adding the getBlockPrefix function to the form class.

Therefore, in the above case, the custom block might look something like this:

 {% block _ProductBundle_product_entry_widget %} <div> {{ form_row(form.field)}} </div> {% endblock %} 

Where 'field' will be the name of the field in your collection item.

0
source

All Articles