sonata-project / SonataAdminBundle

The missing Symfony Admin Generator
https://docs.sonata-project.org/projects/SonataAdminBundle
MIT License
2.11k stars 1.26k forks source link

Error when rendered block with roles in dashboard #2958

Closed tomasDar closed 8 years ago

tomasDar commented 9 years ago

I create tow groups with roles, when first block not show because the user not access the dropdown with new entity create link (outside the user menu ) are broken.

Fixed this problem with next code, in Resources\views\Core\add_block.html.twig

{% block user_block %}
    {% set items_per_column = admin_pool.getOption('dropdown_number_groups_per_colums') %}
    {% set groups = [] %}

    {% for group in admin_pool.dashboardgroups %}
        {% set display_group = false %}

        {% for admin in group.items if display_group == false %}
            {% if admin.hasRoute('create') and admin.isGranted('CREATE') %}
                {% set display_group = true %}
                {% set groups = [group]|merge(groups) %}
            {% endif %}
        {% endfor %}
    {% endfor %}

    {% 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 %}
            >
        {# block UPDATED#}  
        {% set flag=false %}
        {#endblock UPDATED#}
        {% for group in groups|reverse %}
            {% set display = (group.roles is empty or is_granted('ROLE_SONATA_ADMIN') ) %}
            {% for role in group.roles if not display %}
                {% set display = is_granted(role) %}
            {% endfor %}

            {# block UPDATED#}
            {% if loop.first or loop.index0 % items_per_column == 0 %}
                    {% if loop.first %}
                        <div class="container-fluid">
                            <div class="row">
                    {% else %}
                        </ul>
                    {% endif %}

                    <ul class="dropdown-menu{% if column_count > 1 %} col-md-{{ (12/column_count)|round }}{% endif %}">
            {% endif %}
            {#endblock UPDATED#}
            {% if display %}

                {# block UPDATED#}
                {% if flag % items_per_column != 0 %}
                    <li role="presentation" class="divider"></li>
                {% endif %}
                {#endblock UPDATED#}
                <li role="presentation" class="dropdown-header">{{ group.label|trans({}, group.label_catalogue) }}</li>

                {% for admin in group.items %}
                    {% if admin.hasRoute('create') and admin.isGranted('CREATE') %}
                        <li role="presentation">
                            <a role="menuitem" tabindex="-1" href="{{ admin.generateUrl('create')}}">{{ admin.label|trans({}, admin.translationdomain) }}</a>
                        </li>
                    {% endif %}
                {% endfor %}

                {% set flag=true %}
            {% endif %}
            {# block UPDATED#}
            {% if loop.last %}
                            </ul>
                        </div>
                    </div>
            {% endif %}
            {#endblock UPDATED#}
        {% endfor %}
    </div>
{% endblock %}

Sorry for my English, i speak Spanish.

ruby232 commented 9 years ago

I have same problem +1

rande commented 9 years ago

@tomasDar Can you create a PR with the code, it will be easier to get the issue fixed.

flavianh commented 8 years ago

+1 this is quite annoying

soullivaneuh commented 8 years ago

See: https://github.com/sonata-project/SonataAdminBundle/pull/3866#issuecomment-221059328

Please upgrade to 3.x. If the bug is still here, feel free to open another issue.

flavianh commented 8 years ago

Oh so you're not maintaining 2.x for the thousasnds of people that still use it and refuse to let the community help in a bugfix, all right, great thinking

flavianh commented 8 years ago

Since Sonata does not want to take reponsibility in this issue, I suggest to future browsers of this issue to add their own user_block using the one that @tomasDar has submitted (and which is actually working as a fix for 2.x). Have a look at this page which will help you doing so: https://sonata-project.org/bundles/admin/master/doc/reference/templates.html

soullivaneuh commented 8 years ago

Oh so you're not maintaining 2.x for the thousasnds of people that still use it and refuse to let the community help in a bugfix, all right, great thinking

Please before complaining aginst people doing what they can to maintain bundles under free time, be updated about the releases.

3.0 is the old and never released 2.4.x-dev.

And please before complaining of OMG they changed version number on an unstable branch that I should not use in production, pleas read https://github.com/sonata-project/SonataAdminBundle/issues/3731 and #3053.