Sonataadminbundle: Docs: New section: Export functionality

Created on 8 Aug 2013  路  9Comments  路  Source: sonata-project/SonataAdminBundle

When using SonataAdmin your list view shows options to export the data... but there's no mention of this in the current docs. What we need is a new chapter which covers:

  • customisation options, e.g.

    • choose which formats to offer

    • customise the templates used for a given download format

    • control whether you export the current view only or the whole data set

  • how to add a new format
  • how to turn exports off completely
  • how to control who sees these options and can execute the export
  • any other options or relevant info

I'm new to SonataAdmin so I don't know what's possible with exports. I look forward to reading your docs to fill in this gap in my knowledge!

You can either make a ready-to-merge rst file or send me a hyperlink, document file, or detailed comment which I will then reformat and merge into the documentation.

See #1519 for details about the co-ordination of new documentation entries and other ways to contact me.

docs

All 9 comments

to customize the format you need to overwrite those 3 methods:

    /**
     * {@inheritdoc}
     */
    public function getExportFormats()
    {
        return array(
            'json', 'xml', 'csv', 'xls'
        );
    }

    /**
     * @return array
     */
    public function getExportFields()
    {
        return $this->getModelManager()->getExportFields($this->getClass());
    }

    /**
     * @return
     */
    public function getDataSourceIterator()
    {
        $datagrid = $this->getDatagrid();
        $datagrid->buildPager();

        return $this->getModelManager()->getDataSourceIterator($datagrid, $this->getExportFields());
    }

If you want a deeper control on how export are handled, you need to review the CrudController and the sonata.admin.exporter service.

But for example:
As you can see this doesn't give the third parameter that is a dateformat.

public function getDataSourceIterator(DatagridInterface $datagrid, array $fields, $firstResult = null, $maxResult = null)
{
    $datagrid->buildPager();
    $query = $datagrid->getQuery();
    $query->select('DISTINCT ' . $query->getRootAlias());
    $query->setFirstResult($firstResult);
    $query->setMaxResults($maxResult);
        if ($query instanceof ProxyQueryInterface) {
            $query->addOrderBy($query->getSortBy(), $query->getSortOrder());
            $query = $query->getQuery();
        }
    return new DoctrineORMQuerySourceIterator($query, $fields);
} 

:+1:

:+1: - yeap, docs definitely need update - even this time of explanation like @rande wrote here whould be great over there. It took ma a while to figure this out.

I made my custom Writers, custom export service and custom functions to be able to export proper headers ( with translations) and the same columns as visible in datagrid on sonata admin list but it really took me really long while to figure out that data format is set inside DoctrineORMQuerySourceIterator.php

Whole export functionality would benefit from some update, not only from new docs :)

It's quite strange that it exports all columns from entity with column names instead of labels. Not to mention this headers are not translated in any way and that linked entities are not exported at all by default.

I try to get involved in fixing docs a bit when I finish my project.

@krewetka Any changes that you could add some info to the docs about Exports or show examples from your project? I am very interested in that, if I manage to learn I can add the docs myself I just need to know where to start and maybe few examples.

@krewetka, it's quite an obscure feature, but export headers (labels) can be overwritten by using keys in getExportFields method. It can even be translated!!

I don't know where to modify Bundle doc by including this information, but you can code something like the following:

    public function getExportFields()
    {
        return array(
            'field label 1' => 'field1',
            $this->trans('field label 2') => 'field2',
            'field label 3' => 'field3',
            ...
        );
    }

@rodrigobb actually looking at the output of $this->getModelManager()->getExportFields($this->getClass());, I have found that it isn't an associative array as suggested. But thanks for pointing to the right direction. :)

3 years later is not to late I guess :P see #4029

Override getDataSourceIterator in your XxxAdmin controller in this way:

    // I will update `id` value to another mapped value in this function.
    public function getDataSourceIterator()
    {
        $iter = parent::getDataSourceIterator();
        $kv_map = $this->getContainer()->get('xx.service.xx')->getMap();
        $data = [];
        foreach ($iter as $current) {
            // php iterator value can not be updated by reference, so we do below.
            $tmp = $current;
            $tmp['id'] = $kv_map[ $current['id'] ] ?? ' ';
            $data[] = $tmp;
        }

        $iterator = new IteratorCallbackSourceIterator(new \ArrayIterator($data), function($iterator_current) {
            return $iterator_current;
        });

        return $iterator;
    }

It works fine.

Other, your also can override exportAction function in XxxAdminController to rewrite $this->admin->getDataSourceIterator().

Was this page helpful?
0 / 5 - 0 ratings