action_export.rst 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. The Export action
  2. =================
  3. .. note::
  4. This document is a stub representing a new work in progress. If you're reading
  5. this you can help contribute, **no matter what your experience level with Sonata
  6. is**. Check out the `issues on GitHub`_ for more information about how to get involved.
  7. This document will cover the Export action and related configuration options.
  8. Basic configuration
  9. -------------------
  10. Translation
  11. ~~~~~~~~~~~
  12. All field names are translated by default.
  13. An internal mechanism checks if a field matching the translator strategy label exists in the current translation file
  14. and will use the field name as a fallback.
  15. Customizing export
  16. ~~~~~~~~~~~~~~~~~~
  17. To customize available export formats just overwrite AbstractAdmin::getExportFormats()
  18. .. code-block:: php
  19. <?php
  20. // src/AppBundle/Admin/PersonAdmin.php
  21. class PersonAdmin extends AbstractAdmin
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function getExportFormats()
  27. {
  28. return array(
  29. 'json', 'xml', 'csv', 'xls',
  30. );
  31. }
  32. }
  33. If you want to customize the list of fields to export, overwrite AbstractAdmin::getExportFields() like
  34. this:
  35. .. code-block:: php
  36. <?php
  37. // src/AppBundle/Admin/PersonAdmin.php
  38. class PersonAdmin extends AbstractAdmin
  39. {
  40. /**
  41. * @return array
  42. */
  43. public function getExportFields() {
  44. return array(
  45. 'Id' => 'id',
  46. 'First Name' => 'firstName',
  47. 'Last Name' => 'lastName',
  48. 'Contact' => 'contact.phone',
  49. );
  50. }
  51. }
  52. .. note::
  53. Note that you can use `contact.phone` to access the `phone` property of `Contact` entity
  54. To add more customization to your export you can overwrite AbstractAdmin::getDataSourceIterator().
  55. Supposing you want to change date format in your export file. You can do it like this:
  56. .. code-block:: php
  57. <?php
  58. // src/AppBundle/Admin/PersonAdmin.php
  59. class PersonAdmin extends AbstractAdmin
  60. {
  61. public function getDataSourceIterator()
  62. {
  63. $iterator = parent::getDataSourceIterator();
  64. $iterator->setDateTimeFormat('d/m/Y'); //change this to suit your needs
  65. return $iterator;
  66. }
  67. }
  68. .. note::
  69. **TODO**:
  70. * any global (yml) options that affect the export actions
  71. * how to add new export formats
  72. * customising the query used to fetch the results
  73. .. _`issues on Github`: https://github.com/sonata-project/SonataAdminBundle/issues/1519
  74. .. _`the exporter bundle documentation`: https://github.com/sonata-project/exporter/blob/1.x/docs/reference/symfony.rst