Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Bundles
  4. SonataAdminBundle
  5. Search

Search

Edit this page

The admin comes with a basic global search available in the upper navigation menu. The search iterates over admin classes and looks for filters implementing the Sonata\AdminBundle\Search\SearchableFilterInterface interface with the method isSearchEnabled() returning true. If you are using SonataDoctrineORMBundle, the Sonata\DoctrineORMAdminBundle\Filter\StringFilter filter is searchable and relies on a global_search option.

Note

The current implementation can be expensive if you have a lot of entities as the resulting query does a LIKE %query% OR LIKE %query%...

Disabling the search by admin

You can disable the search for a whole admin by setting the global_search attribute to false at your admin definition using the tag sonata.admin.

1
2
3
<service id="app.admin.post" class="App\Admin\PostAdmin">
    <tag name="sonata.admin" global_search="false" model_class="App\Entity\Post" manager_type="orm" group="Content" label="Post"/>
</service>

Customization

Configure the search templates

The main action is using the template @SonataAdmin/Core/search.html.twig. And each search is handled by a block, the template for the block is @SonataAdmin/Block/block_search_result.html.twig.

The default template values can be configured in the configuration section

1
2
3
4
5
6
7
# config/packages/sonata_admin.yaml

sonata_admin:
    templates:
        # other configuration options
        search:              '@SonataAdmin/Core/search.html.twig'
        search_result_block: '@SonataAdmin/Block/block_search_result.html.twig'

You also need to configure the block in the sonata block config

1
2
3
4
5
6
# config/packages/sonata_admin.yaml

sonata_block:
    blocks:
        sonata.admin.block.search_result:
            contexts: [admin]

You can also configure the block template per admin while defining the admin:

1
2
3
4
5
6
7
<service id="app.admin.post" class="App\Admin\PostAdmin">
        <tag name="sonata.admin" model_class="App\Entity\Post" manager_type="orm" group="Content" label="Post"/>
        <call method="setTemplate">
            <argument>search_result_block</argument>
            <argument>@SonataPost/Block/block_search_result.html.twig</argument>
        </call>
    </service>

Configure the default search result actions

In general the search result generates a link to the show action of an item or displayed as a text if the show route is disabled or you haven't the required permission. You can change this behavior by overriding the option

1
2
3
4
5
# config/packages/sonata_admin.yaml

sonata_admin:
    global_search:
        admin_route: edit

Customize visibility of empty result boxes

By default all the admin boxes are shown in search results and it looks like this:

Custom view

We can fade out the boxes that have no results with:

1
2
3
4
5
# config/packages/sonata_admin.yaml

sonata_admin:
    global_search:
        empty_boxes: fade

and it looks like this:

Custom view

The third option is to hide the empty boxes:

1
2
3
4
5
# config/packages/sonata_admin.yaml

sonata_admin:
    global_search:
        empty_boxes: hide

and it looks like this:

Custom view
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    Become certified from home

    Become certified from home

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Version:

    Table of Contents

    • Disabling the search by admin
    • Customization
      • Configure the search templates
      • Configure the default search result actions
      • Customize visibility of empty result boxes

    Symfony footer

    Avatar of Hubert Moreau, a Symfony contributor

    Thanks Hubert Moreau (@hmoreau) for being a Symfony contributor

    2 commits • 182 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony