action_export.rst 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. The Export action
  2. =================
  3. This document will cover the Export action and related configuration options.
  4. Basic configuration
  5. -------------------
  6. If you have registered the ``SonataExporterBundle`` bundle in the kernel of your application,
  7. you can benefit from a lot of flexibility:
  8. * You can configure default exporters globally.
  9. * You can add custom exporters, also globally.
  10. * You can configure every default writer.
  11. See `the exporter bundle documentation`_ for more information.
  12. Translation
  13. ~~~~~~~~~~~
  14. All field names are translated by default.
  15. An internal mechanism checks if a field matching the translator strategy label exists in the current translation file
  16. and will use the field name as a fallback.
  17. Picking which fields to export
  18. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  19. By default, all fields are exported. More accurately, it depends on the
  20. persistence backend you are using, but for instance, the doctrine ORM backend
  21. exports all fields (associations are not exported). If you want to change this
  22. behavior for a specific admin, you can override the ``getExportFields()`` method:
  23. .. code-block:: php
  24. <?php
  25. public function getExportFields()
  26. {
  27. return array('givenName', 'familyName', 'contact.phone');
  28. }
  29. .. note::
  30. Note that you can use `contact.phone` to access the `phone` property of `Contact` entity
  31. You can also tweak the list by creating an admin extension that implements the
  32. ``configureExportFields()`` method.
  33. .. code-block:: php
  34. public function configureExportFields(AdminInterface $admin, array $fields)
  35. {
  36. unset($fields['updatedAt']);
  37. return $fields;
  38. }
  39. Overriding the export formats for a specific admin
  40. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  41. Changing the export formats can be done by defining a ``getExportFormats()``
  42. method in your admin class.
  43. .. code-block:: php
  44. <?php
  45. public function getExportFormats()
  46. {
  47. return array('pdf', 'html');
  48. }
  49. Customizing the query used to fetch the results
  50. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  51. If you want to customize the query used to fetch the results for a specific admin,
  52. you can override the ``getDataSourceIterator()`` method:
  53. .. code-block:: php
  54. <?php
  55. // src/AppBundle/Admin/PersonAdmin.php
  56. class PersonAdmin extends AbstractAdmin
  57. {
  58. public function getDataSourceIterator()
  59. {
  60. $iterator = parent::getDataSourceIterator();
  61. $iterator->setDateTimeFormat('d/m/Y'); //change this to suit your needs
  62. return $iterator;
  63. }
  64. }
  65. .. note::
  66. **TODO**:
  67. * customising the templates used to render the output
  68. * publish the exporter documentation on the project's website and update the link
  69. .. _`the exporter bundle documentation`: https://github.com/sonata-project/exporter/blob/1.x/docs/reference/symfony.rst