Sonata管理员捆绑包:从全局搜索中排除自定义管理员

时间:2019-01-10 09:39:03

标签: symfony sonata-admin

在我的symfony 3.4项目中,我有2个自定义管理员。专门为报告服务而创建。这些管理员没有特定的实体。 对于自定义管理员,我遵循symfony的配方: https://symfony.com/doc/3.x/bundles/SonataAdminBundle/cookbook/recipe_custom_view.html

现在,当通过Sonata全局搜索搜索项目时,在vendor / sonata-project / admin-bundle / src / Resources / views / Core / search.html.twig中出现“类不存在”错误。 此错误与自定义管理员有关。

是否有解决方案可将这些自定义管理员从全局搜索中排除或解决此错误?

管理员:

<?php
namespace MainBundle\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Route\RouteCollection;

class AccessRightsAdmin extends AbstractAdmin 
{
    protected $baseRoutePattern = 'accessrights';
    protected $baseRouteName = 'Accessrights';

    protected function configureRoutes(RouteCollection $collection)
    {
        $collection->clearExcept(array('list'));
        $collection->add('accesRights', 'accessrights');
    }
}

服务

services:         
    system.admin.accessrights:
        class: MainBundle\Admin\AccessRightsAdmin
        arguments: [~, ~, MainBundle:AccessRightsAdmin]
        tags:
            - { name: sonata.admin, manager_type: orm, group: sonata.admin.group.System, label: Accessrights }
        calls:
            - [ setTranslationDomain, [SonataAdminBundle]]
        public: true                  

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,我会把它留在这里以防有人需要它。
解决方案基本上是覆盖 search.html.twig 并从搜索中忽略您想要的管理员,如下所示:

{% extends base_template %}

{% block title %}{{ 'title_search_results'|trans({'%query%': query}, 'SonataAdminBundle') }}{% endblock %}
{% block breadcrumb %}{% endblock %}
{% block content %}
    <h2 class="page-header">{{ 'title_search_results'|trans({'%query%': query}, 'SonataAdminBundle') }}</h2>

    {% if query is defined and query is not same as(false) %}
        {% set count = 0 %}
        <div class="row" data-masonry='{ "itemSelector": ".search-box-item" }'>
            {% for group in groups %}
                {% set display = group.roles is empty or is_granted(sonata_admin.adminPool.getOption('role_super_admin')) or group.roles|filter(role => is_granted(role))|length > 0 %}

                {% if display %}
                    {% for admin in group.items %}
                        {% set count = count + 1 %}
                        {% if admin.code != 'bundle.admin.admin_to_ignore' %}{# in this line right here add the admin you want to ignore in your search #}
                            {% if admin.hasRoute('create') and admin.hasAccess('create') or admin.hasRoute('list') and admin.hasAccess('list') %}
                                {{ sonata_block_render({
                                    'type': 'sonata.admin.block.search_result'
                                }, {
                                    'query': query,
                                    'admin_code': admin.code,
                                    'page': 0,
                                    'per_page': 10,
                                    'icon': group.icon
                                }) }}
                            {% endif %}
                        {% endif %}
                    {% endfor %}
                {% endif %}
            {% endfor %}
        </div>
    {% endif %}

{% endblock %}

要覆盖文件,您需要将其放在以下路径下:
app -> Resources -> SonataAdminBundle -> views -> Core -> search.html.twig