Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

documentation for custom DataSource #1423

Merged
merged 4 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down
52 changes: 52 additions & 0 deletions docs/reference/data_source.rst
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;
virtualize marked this conversation as resolved.
Show resolved Hide resolved

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'