Please follow the below steps to add the new export method (txt) in sales order grid export options.
{vandor_module Path}/view/adminhtml/ui_component/sales_order_grid.xml
<exportButton class="Magento\Ui\Component\ExportButton">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="options" xsi:type="array">
<item name="csv" xsi:type="array">
<item name="value" xsi:type="string">csv</item>
<item name="label" xsi:type="string" translate="true">CSV</item>
<item name="url" xsi:type="string">mui/export/gridToCsv</item>
</item>
<item name="xml" xsi:type="array">
<item name="value" xsi:type="string">xml</item>
<item name="label" xsi:type="string" translate="true">Excel XML</item>
<item name="url" xsi:type="string">mui/export/gridToXml</item>
</item>
<item name="txt" xsi:type="array">
<item name="value" xsi:type="string">txt</item>
<item name="label" xsi:type="string" translate="true">txt</item>
<item name="url" xsi:type="string">{{module}}/export/gridToTxt</item>
</item>
</item>
</item>
</argument>
</exportButton>
{vendor_module Path}\Model\Export\ConvertToTxt.php
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Vendor\Module\Model\Export;
use Magento\Ui\Model\Export\MetadataProvider;
use Magento\Ui\Model\Export\SearchResultIteratorFactory;
use Magento\Framework\Api\Search\DocumentInterface;
use Magento\Framework\Api\Search\SearchResultInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Convert\Excel;
use Magento\Framework\Convert\ExcelFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\WriteInterface;
use Magento\Ui\Component\MassAction\Filter;
/**
* Class ConvertToTxt
*/
class ConvertToTxt
{
/**
* @var WriteInterface
*/
protected $directory;
/**
* @var MetadataProvider
*/
protected $metadataProvider;
/**
* @var ExcelFactory
*/
protected $excelFactory;
/**
* @var array
*/
protected $options;
/**
* @var SearchResultIteratorFactory
*/
protected $iteratorFactory;
/**
* @var array
*/
protected $fields;
/**
* @param Filesystem $filesystem
* @param Filter $filter
* @param MetadataProvider $metadataProvider
* @param ExcelFactory $excelFactory
* @param SearchResultIteratorFactory $iteratorFactory
*/
public function __construct(
Filesystem $filesystem,
Filter $filter,
MetadataProvider $metadataProvider,
ExcelFactory $excelFactory,
SearchResultIteratorFactory $iteratorFactory
) {
$this->filter = $filter;
$this->directory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
$this->metadataProvider = $metadataProvider;
$this->excelFactory = $excelFactory;
$this->iteratorFactory = $iteratorFactory;
}
/**
* Returns Filters with options
*
* @return array
*/
protected function getOptions()
{
if (!$this->options) {
$this->options = $this->metadataProvider->getOptions();
}
return $this->options;
}
/**
* Returns DB fields list
*
* @return array
*/
protected function getFields()
{
if (!$this->fields) {
$component = $this->filter->getComponent();
$this->fields = $this->metadataProvider->getFields($component);
}
return $this->fields;
}
/**
* Returns row data
*
* @param DocumentInterface $document
* @return array
*/
public function getRowData(DocumentInterface $document)
{
return $this->metadataProvider->getRowData($document, $this->getFields(), $this->getOptions());
}
/**
* Returns TXT file
*
* @return array
* @throws LocalizedException
*/
public function getTxtFile()
{
$component = $this->filter->getComponent();
$name = md5(microtime());
$file = 'export/'. $component->getName() . $name . '.txt';
$this->filter->prepareComponent($component);
$this->filter->applySelectionOnTargetProvider();
$component->getContext()->getDataProvider()->setLimit(0, 0);
/** @var SearchResultInterface $searchResult */
$searchResult = $component->getContext()->getDataProvider()->getSearchResult();
/** @var DocumentInterface[] $searchResultItems */
$searchResultItems = $searchResult->getItems();
$this->prepareItems($component->getName(), $searchResultItems);
/** @var SearchResultIterator $searchResultIterator */
$searchResultIterator = $this->iteratorFactory->create(['items' => $searchResultItems]);
/** @var Excel $excel */
$excel = $this->excelFactory->create([
'iterator' => $searchResultIterator,
'rowCallback'=> [$this, 'getRowData'],
]);
$this->directory->create('export');
$stream = $this->directory->openFile($file, 'w+');
$stream->lock();
$excel->setDataHeader($this->metadataProvider->getHeaders($component));
$excel->write($stream, $component->getName() . '.txt');
$stream->unlock();
$stream->close();
return [
'type' => 'filename',
'value' => $file,
'rm' => true // can delete file after use
];
}
/**
* @param string $componentName
* @param array $items
* @return void
*/
protected function prepareItems($componentName, array $items = [])
{
foreach ($items as $document) {
$this->metadataProvider->convertDate($document, $componentName);
}
}
}
{vendor_module Path}\Controller\Adminhtml\Export\GridToTxt.php
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Vendor\Module\Controller\Adminhtml\Export;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Catalog\Model\Export\ConvertToTxt;
use Magento\Framework\App\Response\Http\FileFactory;
/**
* Class Render
*/
class GridToTxt extends Action
{
/**
* @var ConvertToTxt
*/
protected $converter;
/**
* @var FileFactory
*/
protected $fileFactory;
/**
* @param Context $context
* @param ConvertToXml $converter
* @param FileFactory $fileFactory
*/
public function __construct(
Context $context,
ConvertToTxt $converter,
FileFactory $fileFactory
) {
parent::__construct($context);
$this->converter = $converter;
$this->fileFactory = $fileFactory;
}
/**
* Export data provider to XML
*
* @throws \Magento\Framework\Exception\LocalizedException
* @return \Magento\Framework\App\ResponseInterface
*/
public function execute()
{
return $this->fileFactory->create('export.txt', $this->converter->getTxtFile(), 'var');
}
}