From ddba754b9c968113917c21f7cb3fc859aa8dca99 Mon Sep 17 00:00:00 2001 From: Christoph Wieseke Date: Thu, 22 Apr 2021 14:06:19 +0200 Subject: [PATCH 1/4] documentation for custom DataSource --- docs/reference/data_source.rst | 53 ++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 docs/reference/data_source.rst diff --git a/docs/reference/data_source.rst b/docs/reference/data_source.rst new file mode 100644 index 000000000..be72bebd1 --- /dev/null +++ b/docs/reference/data_source.rst @@ -0,0 +1,53 @@ +.. index:: + double: Reference; Export / DataSource + +Export / DataSource +=================== + +When using an admins export feature you might want to modify how dates and times are exported. +This is done by calling ``setDateTimeFormat`` on the data source iterator. + +Here's one way to do it: + +1. Decorate the default Sonata\DoctrineORMAdminBundle\Exporter\DataSource with your own and call ``setDateTimeFormat`` there.:: + + dataSource = $dataSource; + } + + public function createIterator(ProxyQueryInterface $query, array $fields): SourceIteratorInterface + { + /** @var DoctrineORMQuerySourceIterator $iterator */ + $iterator = $this->dataSource->createIterator($query, $fields); + + $iterator->setDateTimeFormat('Y-m-d H:i:s'); + + return $iterator; + } + } + + +2. Add the your service to the services definition.:: + + # config/services.yaml + services: + ... + App\Service\Admin\DecoratingDataSource: + decorates: 'sonata.admin.data_source.orm' + + From e9a19180c756539bd97536d5968065177bfebfbe Mon Sep 17 00:00:00 2001 From: Christoph Wieseke Date: Fri, 23 Apr 2021 10:20:48 +0200 Subject: [PATCH 2/4] satisfy rst linter --- docs/reference/data_source.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/reference/data_source.rst b/docs/reference/data_source.rst index be72bebd1..5e8396fa1 100644 --- a/docs/reference/data_source.rst +++ b/docs/reference/data_source.rst @@ -42,9 +42,8 @@ Here's one way to do it: } -2. Add the your service to the services definition.:: +2. Add the your service in the ``config/services.yaml`` definition.:: - # config/services.yaml services: ... App\Service\Admin\DecoratingDataSource: From ce286b58e0717e8c4b92d9ed9843e3e39599c784 Mon Sep 17 00:00:00 2001 From: Christoph Wieseke Date: Fri, 23 Apr 2021 10:40:04 +0200 Subject: [PATCH 3/4] added data_source to index tree --- docs/index.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/index.rst b/docs/index.rst index 8168a032c..a7ecdd02e 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -18,6 +18,7 @@ The ``Doctrine ORM Admin`` provides services to work with the ``Admin Bundle`` a reference/templates reference/audit reference/query_proxy + reference/data_source reference/troubleshootings .. toctree:: From 1a9c40bda34e4941997f407fe51a529a31ae5e89 Mon Sep 17 00:00:00 2001 From: Christoph Wieseke Date: Fri, 23 Apr 2021 13:04:14 +0200 Subject: [PATCH 4/4] fixed indention --- docs/reference/data_source.rst | 36 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/docs/reference/data_source.rst b/docs/reference/data_source.rst index 5e8396fa1..1d7763f9e 100644 --- a/docs/reference/data_source.rst +++ b/docs/reference/data_source.rst @@ -12,33 +12,33 @@ Here's one way to do it: 1. Decorate the default Sonata\DoctrineORMAdminBundle\Exporter\DataSource with your own and call ``setDateTimeFormat`` there.:: dataSource = $dataSource; - } - - public function createIterator(ProxyQueryInterface $query, array $fields): SourceIteratorInterface - { - /** @var DoctrineORMQuerySourceIterator $iterator */ - $iterator = $this->dataSource->createIterator($query, $fields); + private DataSource $dataSource; - $iterator->setDateTimeFormat('Y-m-d H:i:s'); - - return $iterator; - } + public function __construct(DataSource $dataSource) + { + $this->dataSource = $dataSource; + } + + public function createIterator(ProxyQueryInterface $query, array $fields): SourceIteratorInterface + { + /** @var DoctrineORMQuerySourceIterator $iterator */ + $iterator = $this->dataSource->createIterator($query, $fields); + + $iterator->setDateTimeFormat('Y-m-d H:i:s'); + + return $iterator; + } }