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

[#25963] Fixed grids export: option labels are taken from grid filters and columns now. #26523

Merged
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 91 additions & 26 deletions app/code/Magento/Ui/Model/Export/MetadataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

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 @@ -75,26 +84,29 @@ public function __construct(
* Returns Columns component
*
* @param UiComponentInterface $component
*
* @return UiComponentInterface
* @throws \Exception
* @throws Exception
*/
protected function getColumnsComponent(UiComponentInterface $component)
protected function getColumnsComponent(UiComponentInterface $component): UiComponentInterface
{
foreach ($component->getChildComponents() as $childComponent) {
if ($childComponent instanceof Columns) {
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)
protected function getColumns(UiComponentInterface $component): array
{
if (!isset($this->columns[$component->getName()])) {
$columns = $this->getColumnsComponent($component);
Expand All @@ -104,16 +116,19 @@ protected function getColumns(UiComponentInterface $component)
}
}
}

return $this->columns[$component->getName()];
}

/**
* Retrieve Headers row array for Export
*
* @param UiComponentInterface $component
*
* @return string[]
* @throws Exception
*/
public function getHeaders(UiComponentInterface $component)
public function getHeaders(UiComponentInterface $component): array
{
$row = [];
foreach ($this->getColumns($component) as $column) {
Expand All @@ -127,14 +142,17 @@ public function getHeaders(UiComponentInterface $component)
* Returns DB fields list
*
* @param UiComponentInterface $component
* @return array
*
* @return string[]
* @throws Exception
*/
public function getFields(UiComponentInterface $component)
public function getFields(UiComponentInterface $component): array
{
$row = [];
foreach ($this->getColumns($component) as $column) {
$row[] = $column->getName();
}

return $row;
}

Expand All @@ -144,9 +162,10 @@ public function getFields(UiComponentInterface $component)
* @param DocumentInterface $document
* @param array $fields
* @param array $options
* @return array
*
* @return string[]
*/
public function getRowData(DocumentInterface $document, $fields, $options)
public function getRowData(DocumentInterface $document, $fields, $options): array
{
$row = [];
foreach ($fields as $column) {
Expand All @@ -161,6 +180,7 @@ public function getRowData(DocumentInterface $document, $fields, $options)
$row[] = $document->getCustomAttribute($column)->getValue();
}
}

return $row;
}

Expand All @@ -170,9 +190,10 @@ public function getRowData(DocumentInterface $document, $fields, $options)
* @param array $list
* @param string $label
* @param array $output
*
* @return void
*/
protected function getComplexLabel($list, $label, &$output)
protected function getComplexLabel($list, $label, &$output): void
{
foreach ($list as $item) {
if (!is_array($item['value'])) {
Expand All @@ -184,34 +205,75 @@ 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()
public function getOptions(): array
{
return array_merge(
$this->getColumnOptions(),
$this->getFilterOptions()
);
}

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

return $options;
}

/**
* Get options from column filters.
*
* @return array
* @throws LocalizedException
*/
protected function getFilterOptions(): array
{
$options = [];
$component = $this->filter->getComponent();
Expand All @@ -221,22 +283,25 @@ 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'));
}
}
}
}

return $options;
}

/**
* 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)
public function convertDate($document, $componentName): void
{
if (!isset($this->data[$componentName])) {
return;
Expand All @@ -247,7 +312,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