-
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
documentation for custom DataSource (#1423)
* documentation for custom DataSource * satisfy rst linter * added data_source to index tree * fixed indention
- Loading branch information
1 parent
83ca012
commit 461c588
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
.. 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.:: | ||
|
||
<?php | ||
namespace App\Service\Admin; | ||
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; | ||
use Sonata\AdminBundle\Exporter\DataSourceInterface; | ||
use Sonata\DoctrineORMAdminBundle\Exporter\DataSource; | ||
use Sonata\Exporter\Source\DoctrineORMQuerySourceIterator; | ||
use Sonata\Exporter\Source\SourceIteratorInterface; | ||
class DecoratingDataSource implements DataSourceInterface | ||
{ | ||
private DataSource $dataSource; | ||
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; | ||
} | ||
} | ||
|
||
|
||
2. Add the your service in the ``config/services.yaml`` definition.:: | ||
|
||
services: | ||
... | ||
App\Service\Admin\DecoratingDataSource: | ||
decorates: 'sonata.admin.data_source.orm' | ||
|
||
|