Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages showcasing Symfony with Docker, APIs, queues & async tasks, Webpack, SPAs, etc.

19. Persisting Filters

4.x version

19. Persisting Filters

Persisting filters allow your application to save the filters the authenticated user has submitted. Then the saved filters will be reused if the page is displayed again.

19.1. Enable Filters Persistence

By default, filters persistence is disabled. You can enable it in your sonata_admin configuration :

  • YAML
    1
    2
    3
    4
    # config/packages/sonata_admin.yaml
    
    sonata_admin:
        persist_filters: true
    

19.2. Choose the persistence strategy

When you enable the filters persistence by setting persist_filters to true. SonataAdmin will use the default filter persister : Sonata\AdminBundle\Filter\Persister\SessionFilterPersister (which is, by now, the only one provided).

You can implement your own filter persister by creating a new class that implements the Sonata\AdminBundle\Filter\Persister\FilterPersisterInterface interface and registering it as a service. Then the only thing to do is to tell SonataAdmin to use this service as filter persister.

Globally :

  • YAML
    1
    2
    3
    4
    5
    # config/packages/sonata_admin.yaml
    
    sonata_admin:
        persist_filters: true
        filter_persister: filter_persister_service_id
    

Per Admin :

  • YAML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    # config/services.yaml
    
    services:
        app.admin.user:
            class: App\Admin\UserAdmin
            arguments:
                - ~
                - App\Entity\User
                - ~
            tags:
                -
                    name: sonata.admin
                    manager_type: orm
                    filter_persister: filter_persister_service_id
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    <!-- config/services.xml -->
    
    <service id="app.admin.user" class="App\Admin\UserAdmin">
        <argument/>
        <argument>App\Entity\User</argument>
        <argument/>
        <tag
            name="sonata.admin"
            manager_type="orm"
            filter_persister="filter_persister_service_id"
            />
    </service>
    

19.3. Disable filters persistence for some Admin

When you enable the filters persistence by setting persist_filters to true. All registered Admins will have the feature enabled.

You can disable it per Admin if you want.

  • YAML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    # config/services.yaml
    
    services:
        app.admin.user:
            class: App\Admin\UserAdmin
            arguments:
                - ~
                - App\Entity\User
                - ~
            tags:
                - { name: sonata.admin, manager_type: orm, persist_filters: false }
    
  • XML
    1
    2
    3
    4
    5
    6
    7
    8
    <!-- config/services.xml -->
    
    <service id="app.admin.user" class="App\Admin\UserAdmin">
        <argument/>
        <argument>App\Entity\User</argument>
        <argument/>
        <tag name="sonata.admin" manager_type="orm" persist_filters="false"/>
    </service>
    

Note

Both persist_filters and filter_persister can be used globally and per-admin, which provide you the most flexible way to configure this feature.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.