score:3

Accepted answer

If you want to do a custom query for your list view, you could override the createQuery method in your Admin class like this :

class EntityAdmin
{
    public function createQuery($context = 'list')
    {
        $query = parent::createQuery($context);
        $query->andWhere(
            $query->expr()->eq($query->getRootAlias() . '.id', ':id')
        );
        $query->setParameter('id', 1);

        return $query;
    }
}

You will have only your first entity in your list view.

UPDATE

You could override the standard_layout.html.twig to change the link in your sidebar :

First you need to set where your template is located:

app/config/config.yml

sonata_admin:
    templates:
        layout: ApplicationSonataAdminBundle::standard_layout.html.twig

Change the behaviour of the sidebar, for Sonata Admin 2.3 this is how you do it :

src/Application/Sonata/AdminBundle/Resources/Views/standard_layout.html.twig l.224

<ul class="treeview-menu{% if active %} active{% endif %}">
    {% for admin in group.items %}
        {% if admin.code == 'sonata.admin.entity' and 
              admin.hasroute('edit') and 
              admin.isGranted('EDIT') %}
            <li{% if app.request.get('_sonata_admin') == admin.code %} class="active"{% endif %}><a href="{{ admin.generateUrl('edit', {'id' : 1}) }}"><i class="fa fa-angle-double-right"></i> {{ admin.label|trans({}, admin.translationdomain) }}</a></li>
        {% else %}
            {% if admin.hasroute('list') and admin.isGranted('LIST') %}
                <li{% if app.request.get('_sonata_admin') == admin.code %} class="active"{% endif %}><a href="{{ admin.generateUrl('list')}}"><i class="fa fa-angle-double-right"></i> {{ admin.label|trans({}, admin.translationdomain) }}</a></li>
            {% endif %}
        {% endif %}
    {% endfor %}
</ul>

You must change 'sonata.admin.entity' by the identifier of your admin service.

Also if you want to remove access to the list you should add in your Admin class

use Sonata\AdminBundle\Route\RouteCollection;

class EntityAdmin
{
    protected function configureRoutes(RouteCollection $collection)
    {
        $collection->remove('list');
    }
}

Related Query

More Query from same tag