search.rst 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. Search
  2. ======
  3. The admin comes with a basic global search available in the upper navigation menu. The search iterates over admin classes
  4. and look for filter with the option ``global_search`` set to true. If you are using the ``SonataDoctrineORMBundle``
  5. any text filter will be set to ``true`` by default.
  6. Customization
  7. -------------
  8. The main action is using the template ``SonataAdminBundle:Core:search.html.twig``. And each search is handled by a
  9. ``block``, the template for the block is ``SonataAdminBundle:Block:block_search_result.html.twig``.
  10. The default template values can be configured in the configuration section
  11. .. configuration-block::
  12. .. code-block:: yaml
  13. # app/config/config.yml
  14. sonata_admin:
  15. templates:
  16. # other configuration options
  17. search: SonataAdminBundle:Core:search.html.twig
  18. search_result_block: SonataAdminBundle:Block:block_search_result.html.twig
  19. You also need to configure the block in the sonata block config
  20. .. configuration-block::
  21. .. code-block:: yaml
  22. # app/config/config.yml
  23. sonata_block:
  24. blocks:
  25. sonata.admin.block.search_result:
  26. contexts: [admin]
  27. You can also configure the block template per admin while defining the admin:
  28. .. configuration-block::
  29. .. code-block:: xml
  30. <service id="app.admin.post" class="AppBundle\Admin\PostAdmin">
  31. <tag name="sonata.admin" manager_type="orm" group="Content" label="Post" />
  32. <argument />
  33. <argument>AppBundle\Entity\Post</argument>
  34. <argument />
  35. <call method="setTemplate">
  36. <argument>search_result_block</argument>
  37. <argument>SonataPostBundle:Block:block_search_result.html.twig</argument>
  38. </call>
  39. </service>
  40. Configure the default search result action
  41. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  42. In general the search result generates a link to the edit action of an item or is using the show action, if the edit
  43. route is disabled or you haven't the required permission. You can change this behaviour by overriding the
  44. ``searchResultActions`` property. The defined action list will we checked successive until a route with the required
  45. permissions exists. If no route is found, the item will be displayed as a text.
  46. .. code-block:: php
  47. <?php
  48. // src/AppBundle/Admin/PersonAdmin.php
  49. class PersonAdmin extends AbstractAdmin
  50. {
  51. // ...
  52. protected $searchResultActions = array('edit', 'show');
  53. // ...
  54. }
  55. Performance
  56. -----------
  57. The current implementation can be expensive if you have a lot of entities as the resulting query does a ``LIKE %query% OR LIKE %query%``...
  58. .. note::
  59. There is a work in progress to use an async JavaScript solution to better load data from the database.