{% set items_per_column = sonata_config.getOption('dropdown_number_groups_per_colums') %}

{% set groups = get_sonata_dashboard_groups_with_creatable_admins() %}

{% set column_count = (groups|length / items_per_column)|round(0, 'ceil') %}

<div class="dropdown-menu multi-column dropdown-add"
    {% if column_count > 1 %}style="width: {{ column_count*140 }}px;"{% endif %}
>
    {% for group in groups|reverse %}
        {% set display = group.roles is empty or is_granted(sonata_config.getOption('role_admin')) or group.roles|filter(role => is_granted(role))|length > 0 %}

        {% if loop.first %}
            {% set render_first_element = true %}
        {% endif %}

        {% if display %}
            {% if render_first_element or loop.index0 % items_per_column == 0 %}
                {% if render_first_element %}
                    <div class="container-fluid">
                        <div class="row">
                    {% set render_first_element = false %}
                {% else %}
                    </ul>
                {% endif %}
                <ul class="dropdown-menu{% if column_count > 1 %} col-md-{{ (12/column_count)|round }}{% endif %}">
            {% endif %}
            {% if loop.index0 % items_per_column != 0 %}
                <li role="presentation" class="divider"></li>
            {% endif %}
            <li role="presentation" class="dropdown-header">
                {{ group.icon|parse_icon }}
                {# NEXT_MAJOR: Remove the default null #}
                {% if group.translation_domain|default(null) is same as(false) %}
                    {{ group.label }}
                {% else %}
                    {# NEXT_MAJOR: Remove the fallback on group.label_catalogue #}
                    {{ group.label|trans({}, group.translation_domain|default(group.label_catalogue)) }}
                {% endif %}
            </li>

            {% for admin in group.items %}
                {% if admin.hasRoute('create') and admin.hasAccess('create') %}
                    {% if admin.subClasses is empty %}
                        <li role="presentation">
                            <a role="menuitem" tabindex="-1" href="{{ admin.generateUrl('create') }}">
                                {% if admin.label is not empty %}
                                    {{ admin.label|trans({}, admin.translationdomain) }}
                                {% endif %}
                            </a>
                        </li>
                    {% else %}
                        {% for subclass in admin.subclasses|keys %}
                            <li role="presentation">
                                <a role="menuitem" tabindex="-1" href="{{ admin.generateUrl('create', {'subclass': subclass}) }}">
                                    {{ subclass|trans({}, admin.translationdomain) }}
                                </a>
                            </li>
                        {% endfor %}
                    {% endif %}
                {% endif %}
            {% endfor %}
            {% if loop.last and not render_first_element %}
                </ul>
                </div>
                </div>
            {% endif %}
        {% endif %}
    {% endfor %}
</div>
