0
votes

Within my Symfony 3.4 project, I have 2 custom admins. Specially created for reporting services. Those admins do not have specific entities. For the custom admins, I followed the Symfony recipe: https://symfony.com/doc/3.x/bundles/SonataAdminBundle/cookbook/recipe_custom_view.html

Now, when searching items through the sonata global search, I get a

"Class does not exist" error in vendor/sonata-project/admin-bundle/src/Resources/views/Core/search.html.twig.

This error is related to the custom admins.

Is there a solution to exclude these custom admins from the global search or to resolve this error?

Admin:

<?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');
    }
}

Service

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
pls post your service definition and your admin class - Fabien Papet
Added the code to the post. - de_bernie
@de_bernie How did you manage to solve this ? - Assil
@de_bernie: Did you find a solution? My workaround is to overwrite the search.html.twig and exclude the specific admin in the if-block. - borschtel1

1 Answers

0
votes

I found a solution and I'm going to leave it here in case someone need it.
The solution basically is to override the search.html.twig and ignore the admin you want from the search like so:

{% 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 %}

To override the file you need to put it under the following path:
app -> Resources -> SonataAdminBundle -> views -> Core -> search.html.twig