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

Docs: New section: Export functionality #1550

Closed
caponica opened this issue Aug 8, 2013 · 9 comments
Closed

Docs: New section: Export functionality #1550

caponica opened this issue Aug 8, 2013 · 9 comments
Labels

Comments

@caponica
Copy link
Contributor

caponica commented Aug 8, 2013

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.

@rande
Copy link
Member

rande commented Aug 9, 2013

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.

@FraGoTe
Copy link

FraGoTe commented Mar 12, 2014

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);
} 

@webdevilopers
Copy link
Contributor

👍

@krewetka
Copy link
Contributor

👍 - 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.

@ethernal
Copy link

@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.

@rodrigobb
Copy link

@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',
            ...
        );
    }

@thoroc
Copy link

thoroc commented Mar 25, 2016

@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. :)

@greg0ire
Copy link
Contributor

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

@farwish
Copy link

farwish commented Mar 6, 2018

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().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests