{% block collection_widget -%}
    {% from 'SyliusResourceBundle:Macros:notification.html.twig' import error %}
    {% set attr = attr|merge({'class': attr.class|default ~ ' controls collection-widget'}) %}

    {% spaceless %}
        <div data-form-type="collection"
             {{ block('widget_container_attributes') }}
             {% if prototype is not empty %}
             data-prototype='{{ _self.collectionItem(prototype, allow_delete, button_delete_label, '__name__')|e }}'
             {% endif %}>

            {{ error(form.vars.errors) }}

            {% if prototypes|default is iterable %}
                {% for key, subPrototype in prototypes %}
                    <input type="hidden"
                           data-form-prototype="{{ key }}"
                           value="{{ _self.collectionItem(subPrototype, allow_delete, button_delete_label, '__name__')|e }}" />
                {% endfor %}
            {% endif %}

            <div data-form-collection="list"
                 class="row collection-list">
                {% for child in form %}
                    {{ _self.collectionItem(child, allow_delete, button_delete_label, loop.index0) }}
                {% endfor %}
            </div>

            {% if prototype is defined and allow_add %}
                <a href="#" class="btn btn-success btn-block"
                   data-form-collection="add">
                    <i class="glyphicon glyphicon-plus"></i>
                    {{ button_add_label|trans }}
                </a>
            {% endif %}
        </div>
    {% endspaceless %}
{%- endblock collection_widget %}

{% macro collectionItem(form, allow_delete, button_delete_label, index) %}
    {% spaceless %}
        <div data-form-collection="item"
             data-form-collection-index="{{ index }}"
             class="collection-item">
            <div class="collection-box{% if form|length == 1%} unique-field{% endif %}">
                {% if allow_delete %}
                    <p class="text-right">
                        <a href="#" data-form-collection="delete" class="btn btn-danger">
                            <i class="glyphicon glyphicon-remove collection-button-remove"></i>
                            {{ button_delete_label|trans }}
                        </a>
                    </p>
                {% endif %}

                {{ form_rest(form) }}
            </div>
        </div>
    {% endspaceless %}
{% endmacro %}