Skip to content

Commit

Permalink
Grid Export rendered data is not reflecting in the exported File, Dis…
Browse files Browse the repository at this point in the history
…played ID instead of Rendered Label #25963.

Fixed grids export: option labels are taken from grid filters and columns now.
  • Loading branch information
novikor committed Jan 24, 2020
1 parent 0ac3443 commit b43980b
Showing 1 changed file with 70 additions and 16 deletions.
86 changes: 70 additions & 16 deletions app/code/Magento/Ui/Model/Export/MetadataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Ui\Model\Export;

use DateTime;
use DateTimeZone;
use Exception;
use Magento\Framework\Api\Search\DocumentInterface;
use Magento\Framework\Data\OptionSourceInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Framework\View\Element\UiComponentInterface;
use Magento\Ui\Component\Filters;
use Magento\Ui\Component\Filters\Type\Select;
use Magento\Ui\Component\Listing\Columns;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;

/**
* Metadata Provider
* Metadata Provider for grid listing export.
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class MetadataProvider
Expand Down Expand Up @@ -76,7 +84,7 @@ public function __construct(
*
* @param UiComponentInterface $component
* @return UiComponentInterface
* @throws \Exception
* @throws Exception
*/
protected function getColumnsComponent(UiComponentInterface $component)
{
Expand All @@ -85,14 +93,16 @@ protected function getColumnsComponent(UiComponentInterface $component)
return $childComponent;
}
}
throw new \Exception('No columns found'); // @codingStandardsIgnoreLine
throw new Exception('No columns found'); // @codingStandardsIgnoreLine
}

/**
* Returns columns list
*
* @param UiComponentInterface $component
*
* @return UiComponentInterface[]
* @throws Exception
*/
protected function getColumns(UiComponentInterface $component)
{
Expand All @@ -111,7 +121,9 @@ protected function getColumns(UiComponentInterface $component)
* Retrieve Headers row array for Export
*
* @param UiComponentInterface $component
*
* @return string[]
* @throws Exception
*/
public function getHeaders(UiComponentInterface $component)
{
Expand All @@ -127,7 +139,9 @@ public function getHeaders(UiComponentInterface $component)
* Returns DB fields list
*
* @param UiComponentInterface $component
*
* @return array
* @throws Exception
*/
public function getFields(UiComponentInterface $component)
{
Expand Down Expand Up @@ -184,34 +198,73 @@ protected function getComplexLabel($list, $label, &$output)
}

/**
* Returns array of Select options
* Prepare array of options.
*
* @param array $options
*
* @param Select $filter
* @return array
*/
protected function getFilterOptions(Select $filter)
protected function getOptionsArray(array $options): array
{
$options = [];
foreach ($filter->getData('config/options') as $option) {
$preparedOptions = [];
foreach ($options as $option) {
if (!is_array($option['value'])) {
$options[$option['value']] = $option['label'];
$preparedOptions[$option['value']] = $option['label'];
} else {
$this->getComplexLabel(
$option['value'],
$option['label'],
$options
$preparedOptions
);
}
}
return $options;

return $preparedOptions;
}

/**
* Returns Filters with options
*
* @return array
* @throws LocalizedException
*/
public function getOptions()
{
return array_merge(
$this->getColumnOptions(),
$this->getFilterOptions()
);
}

/**
* Get options from columns.
*
* @return array
* @throws LocalizedException
* @throws Exception
*/
protected function getColumnOptions()
{
$options = [];
$component = $this->filter->getComponent();
/** @var Column $columnComponent */
foreach ($this->getColumns($component) as $columnComponent) {
/** @var OptionSourceInterface $options */
if ($optionSource = $columnComponent->getData('options')) {
$options[$columnComponent->getName()] = $this->getOptionsArray($optionSource->toOptionArray());
}
}

return $options;
}

/**
* Get options from column filters.
*
* @return array
* @throws LocalizedException
*/
protected function getFilterOptions()
{
$options = [];
$component = $this->filter->getComponent();
Expand All @@ -221,7 +274,7 @@ public function getOptions()
if ($child instanceof Filters) {
foreach ($child->getChildComponents() as $filter) {
if ($filter instanceof Select) {
$options[$filter->getName()] = $this->getFilterOptions($filter);
$options[$filter->getName()] = $this->getOptionsArray($filter->getData('config/options'));
}
}
}
Expand All @@ -232,9 +285,10 @@ public function getOptions()
/**
* Convert document date(UTC) fields to default scope specified
*
* @param \Magento\Framework\Api\Search\DocumentInterface $document
* @param DocumentInterface $document
* @param string $componentName
* @return void
* @throws Exception
*/
public function convertDate($document, $componentName)
{
Expand All @@ -247,7 +301,7 @@ public function convertDate($document, $componentName)
continue;
}
$convertedDate = $this->localeDate->date(
new \DateTime($fieldValue, new \DateTimeZone('UTC')),
new DateTime($fieldValue, new DateTimeZone('UTC')),
$this->locale,
true
);
Expand Down

0 comments on commit b43980b

Please sign in to comment.