4

I have an options array in below format.

array (size=3)
  0 => 
    array (size=2)
      'value' => string 'one' (length=3)
      'label' => string 'Number One' (length=10)
  1 => 
    array (size=2)
      'value' => string 'two' (length=3)
      'label' => string 'Number Two' (length=10)
  2 => 
    array (size=2)
      'value' => string 'three' (length=5)
      'label' => string 'Number Three' (length=12)

In admin grid column data label is appearing, which is correct.
But when I am exporting the same grid the instead of data label, the data value is appearing the exported CSV and XML.
The Attribute is of type select.
Code is ui_component xml is as follows

<column name="mycol" component="Magento_Ui/js/grid/columns/select">
            <settings>
                <options class="Anshu\Mygrid\Model\Mygrid\Source\Myvalue"/>
                <filter>select</filter>
                <dataType>select</dataType>
                <label translate="true">My Value</label>
            </settings>
        </column>

What can be the issue here?

2
  • Did you find any solution for this ? Commented Feb 5, 2019 at 7:26
  • @PankajPareek No, I didn't found any solution for this. Kindly post it here if you get any solution. Commented Feb 5, 2019 at 7:43

1 Answer 1

0

Here is the solution which I have used in my custom module.

In this file add below line on code:

app/code/Custom/Module/etc/di.xml

<preference for="Magento\Ui\Model\Export\ConvertToCsv" type="Custom\Module\Model\Export\ConvertToCsv" />

Now create a new file and read the comment which I have mentioned in my code which you have replace as per your requirement.

app/code/Custom/Module/Model/Export/ConvertToCsv.php

    <?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Custom\Module\Model\Export;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\Ui\Component\MassAction\Filter;

/**
 * Class ConvertToCsv
 */
class ConvertToCsv extends \Magento\Ui\Model\Export\ConvertToCsv
{
    /**
     * @var DirectoryList
     */
    protected $directory;

    /**
     * @var MetadataProvider
     */
    protected $metadataProvider;

    /**
     * @var int|null
     */
    protected $pageSize = null;

    /**
     * @var Filter
     */
    protected $filter;

    /**
     * @param Filesystem $filesystem
     * @param Filter $filter
     * @param MetadataProvider $metadataProvider
     * @param int $pageSize
     * @throws FileSystemException
     */
    public function __construct(
        Filesystem $filesystem,
        Filter $filter,
        \Magento\Ui\Model\Export\MetadataProvider $metadataProvider,
        $pageSize = 200
    ) {
        $this->filter           = $filter;
        $this->directory        = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
        $this->metadataProvider = $metadataProvider;
        $this->pageSize         = $pageSize;

        parent::__construct($filesystem, $filter, $metadataProvider, $pageSize);
    }

    /**
     * Returns CSV file
     *
     * @return array
     * @throws LocalizedException
     */
    public function getCsvFile()
    {
        $component = $this->filter->getComponent();

        $name = md5(microtime());
        $file = 'export/'. $component->getName() . $name . '.csv';

        $this->filter->prepareComponent($component);
        $this->filter->applySelectionOnTargetProvider();
        $dataProvider = $component->getContext()->getDataProvider();
        $fields = $this->metadataProvider->getFields($component);
        $options = $this->metadataProvider->getOptions();

        $this->directory->create('export');
        $stream = $this->directory->openFile($file, 'w+');
        $stream->lock();
        $stream->writeCsv($this->metadataProvider->getHeaders($component));
        $i = 1;
        $searchCriteria = $dataProvider->getSearchCriteria()
            ->setCurrentPage($i)
            ->setPageSize($this->pageSize);
        $totalCount = (int) $dataProvider->getSearchResult()->getTotalCount();

        while ($totalCount > 0) {
            $items = $dataProvider->getSearchResult()->getItems();
            foreach ($items as $item) {
                if($component->getName() == 'custom_item_listing') { // replace this listing by your listing.xml file name
                    $item->setCustomValue('test'); // this will be your custom attribute/column which needs to be exported as per your requirement.
                }
                $this->metadataProvider->convertDate($item, $component->getName());
                $stream->writeCsv($this->metadataProvider->getRowData($item, $fields, $options));
            }
            $searchCriteria->setCurrentPage(++$i);
            $totalCount = $totalCount - $this->pageSize;
        }
        $stream->unlock();
        $stream->close();

        return [
            'type' => 'filename',
            'value' => $file,
            'rm' => true  // can delete file after use
        ];
    }
}

This solution is working fine for me. Hope this will work for you as well.

Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.