Skip to content

Commit 5547cbc

Browse files
Merge branch 'MAGETWO-38563' of https://github.corp.ebay.com/magento-south/magento2ce into MAGETWO-40747
2 parents 7b83c05 + f4e3f91 commit 5547cbc

File tree

8 files changed

+129
-32
lines changed

8 files changed

+129
-32
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Model\Plugin\Grid;
7+
8+
use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;
9+
use Magento\Customer\Ui\Component\Listing\AttributeRepository;
10+
use Magento\Customer\Api\Data\AttributeMetadataInterface;
11+
12+
class CustomAttribute
13+
{
14+
/**
15+
* @var AttributeRepository
16+
*/
17+
protected $attributeRepository;
18+
19+
/**
20+
* @param AttributeRepository $attributeRepository
21+
*/
22+
public function __construct(
23+
AttributeRepository $attributeRepository
24+
) {
25+
$this->attributeRepository = $attributeRepository;
26+
}
27+
28+
/**
29+
* @return AttributeMetadataInterface[]
30+
*/
31+
public function getCustomAttributesWithOptions()
32+
{
33+
$attributes = [];
34+
foreach ($this->attributeRepository->getList() as $attribute) {
35+
if ($attribute->getBackendType() != 'static' && $attribute->getIsUsedInGrid() && $attribute->getOptions()) {
36+
$attributes[] = $attribute;
37+
}
38+
}
39+
return $attributes;
40+
}
41+
42+
/**
43+
* @param AttributeMetadataInterface $attribute
44+
* @param string $optionId
45+
* @return string
46+
*/
47+
protected function getAttributeOptionLabelById(AttributeMetadataInterface $attribute, $optionId)
48+
{
49+
$optionLabel = '';
50+
foreach ($attribute->getOptions() as $option) {
51+
if ($option->getValue() === $optionId) {
52+
$optionLabel = $option->getLabel();
53+
break;
54+
}
55+
}
56+
return $optionLabel;
57+
}
58+
59+
/**
60+
* @param SearchResult $subject
61+
* @param \Magento\Framework\View\Element\UiComponent\DataProvider\Document[] $documents
62+
* @return \Magento\Framework\View\Element\UiComponent\DataProvider\Document[]
63+
*
64+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
65+
*/
66+
public function afterGetItems(SearchResult $subject, $documents)
67+
{
68+
/** @var AttributeMetadataInterface $attribute */
69+
foreach ($this->getCustomAttributesWithOptions() as $attribute) {
70+
/** @var \Magento\Framework\View\Element\UiComponent\DataProvider\Document $document */
71+
foreach ($documents as $document) {
72+
$optionId = $document->getData($attribute->getAttributeCode());
73+
if ($optionId) {
74+
$document->setData(
75+
$attribute->getAttributeCode(),
76+
$this->getAttributeOptionLabelById($attribute, $optionId)
77+
);
78+
}
79+
}
80+
}
81+
82+
return $documents;
83+
}
84+
}

app/code/Magento/Customer/Ui/Component/ColumnFactory.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class ColumnFactory
1717
*/
1818
protected $jsComponentMap = [
1919
'text' => 'Magento_Ui/js/grid/columns/column',
20-
'select' => 'Magento_Ui/js/grid/columns/select',
2120
'date' => 'Magento_Ui/js/grid/columns/date',
2221
];
2322

@@ -27,17 +26,18 @@ class ColumnFactory
2726
protected $dataTypeMap = [
2827
'default' => 'text',
2928
'text' => 'text',
30-
'boolean' => 'select',
31-
'select' => 'select',
32-
'multiselect' => 'select',
29+
'boolean' => 'text',
30+
'select' => 'text',
31+
'multiselect' => 'text',
3332
'date' => 'date',
3433
];
3534

3635
/**
3736
* @param \Magento\Framework\View\Element\UiComponentFactory $componentFactory
3837
*/
39-
public function __construct(\Magento\Framework\View\Element\UiComponentFactory $componentFactory)
40-
{
38+
public function __construct(
39+
\Magento\Framework\View\Element\UiComponentFactory $componentFactory
40+
) {
4141
$this->componentFactory = $componentFactory;
4242
}
4343

@@ -55,9 +55,7 @@ public function create($attribute, $context, array $config = [])
5555
'label' => __($attribute->getFrontendLabel()),
5656
'dataType' => $this->getDataType($attribute),
5757
'align' => 'left',
58-
'add_field' => true,
59-
'visible' => $attribute->getIsVisibleInGrid(),
60-
'handler' => 'Magento\Customer\Ui\Component\Listing\Column\AttributeColumn',
58+
'visible' => (bool)$attribute->getIsVisibleInGrid(),
6159
], $config);
6260

6361
if ($attribute->getOptions()) {

app/code/Magento/Customer/Ui/Component/Listing/AttributeRepository.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ public function getList()
8383
return $this->attributes;
8484
}
8585

86+
/**
87+
* @param string $code
88+
* @return AttributeMetadataInterface|null
89+
*/
90+
public function getMetadataByCode($code)
91+
{
92+
$result = null;
93+
foreach ($this->getList() as $attribute) {
94+
if ($attribute->getAttributeCode() == $code) {
95+
$result = $attribute;
96+
break;
97+
}
98+
}
99+
return $result;
100+
}
101+
86102
/**
87103
* @param Attribute $attribute
88104
* @return AttributeMetadataInterface|null

app/code/Magento/Customer/Ui/Component/Listing/Column/AttributeColumn.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,29 @@
88
use Magento\Framework\View\Element\UiComponent\ContextInterface;
99
use Magento\Framework\View\Element\UiComponentFactory;
1010
use Magento\Ui\Component\Listing\Columns\Column;
11-
use Magento\Customer\Api\MetadataInterface;
11+
use Magento\Customer\Ui\Component\Listing\AttributeRepository;
1212

1313
class AttributeColumn extends Column
1414
{
15-
/** @var MetadataInterface */
16-
protected $metadata;
15+
/** @var AttributeRepository */
16+
protected $attributeRepository;
1717

1818
/**
1919
* @param ContextInterface $context
2020
* @param UiComponentFactory $uiComponentFactory
21-
* @param MetadataInterface $metadata
21+
* @param AttributeRepository $attributeRepository
2222
* @param array $components
2323
* @param array $data
2424
*/
2525
public function __construct(
2626
ContextInterface $context,
2727
UiComponentFactory $uiComponentFactory,
28-
MetadataInterface $metadata,
28+
AttributeRepository $attributeRepository,
2929
array $components = [],
3030
array $data = []
3131
) {
3232
parent::__construct($context, $uiComponentFactory, $components, $data);
33-
$this->metadata = $metadata;
33+
$this->attributeRepository = $attributeRepository;
3434
}
3535

3636
/**
@@ -48,13 +48,13 @@ public function prepareDataSource(array &$dataSource)
4848
$attributeCode = isset($this->getData('config')['origin'])
4949
? $this->getData('config')['origin']
5050
: $this->getName();
51-
$options = $this->metadata->getAttributeMetadata($attributeCode)->getOptions();
52-
if (count($options)) {
51+
$metaData = $this->attributeRepository->getMetadataByCode($attributeCode);
52+
if ($metaData && count($metaData->getOptions())) {
5353
foreach ($dataSource['data']['items'] as &$item) {
5454
if (!isset($item[$this->getName()])) {
5555
continue;
5656
}
57-
foreach ($options as $option) {
57+
foreach ($metaData->getOptions() as $option) {
5858
if ($option->getValue() == $item[$this->getName()]) {
5959
$item[$this->getName()] = $option->getLabel();
6060
break;

app/code/Magento/Customer/Ui/Component/Listing/Columns.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ class Columns extends \Magento\Ui\Component\Listing\Columns
1616
*/
1717
protected $columnSortOrder;
1818

19+
/**
20+
* @var AttributeRepository
21+
*/
22+
protected $attributeRepository;
23+
1924
/**
2025
* @param ContextInterface $context
2126
* @param ColumnFactory $columnFactory

app/code/Magento/Customer/etc/adminhtml/di.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@
1313
</arguments>
1414
</type>
1515
<preference for="Magento\Framework\Session\SessionManagerInterface" type="Magento\Backend\Model\Session" />
16+
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
17+
<plugin name="customer-grid-custom-attribute-converter"
18+
type="Magento\Customer\Model\Plugin\Grid\CustomAttribute"/>
19+
</type>
1620
</config>

app/code/Magento/Customer/etc/di.xml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -297,14 +297,4 @@
297297
<argument name="collection" xsi:type="object" shared="false">Magento\Customer\Model\Resource\Address\Attribute\Collection</argument>
298298
</arguments>
299299
</type>
300-
<virtualType name="CustomerAttributeColumn" type="Magento\Customer\Ui\Component\Listing\Column\AttributeColumn">
301-
<arguments>
302-
<argument name="metadata" xsi:type="object">Magento\Customer\Model\Metadata\CustomerMetadata</argument>
303-
</arguments>
304-
</virtualType>
305-
<virtualType name="CustomerAddressAttributeColumn" type="Magento\Customer\Ui\Component\Listing\Column\AttributeColumn">
306-
<arguments>
307-
<argument name="metadata" xsi:type="object">Magento\Customer\Model\Metadata\AddressMetadata</argument>
308-
</arguments>
309-
</virtualType>
310300
</config>

app/code/Magento/Customer/view/adminhtml/ui_component/customer_listing.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@
370370
</item>
371371
</argument>
372372
</column>
373-
<column name="group_id" class="CustomerAttributeColumn">
373+
<column name="group_id" class="Magento\Customer\Ui\Component\Listing\Column\AttributeColumn">
374374
<argument name="data" xsi:type="array">
375375
<item name="js_config" xsi:type="array">
376376
<item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
@@ -411,7 +411,7 @@
411411
</item>
412412
</argument>
413413
</column>
414-
<column name="country_id" class="CustomerAddressAttributeColumn">
414+
<column name="country_id" class="Magento\Customer\Ui\Component\Listing\Column\AttributeColumn">
415415
<argument name="data" xsi:type="array">
416416
<item name="js_config" xsi:type="array">
417417
<item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>
@@ -451,7 +451,7 @@
451451
</item>
452452
</argument>
453453
</column>
454-
<column name="website_id" class="CustomerAttributeColumn">
454+
<column name="website_id" class="Magento\Customer\Ui\Component\Listing\Column\AttributeColumn">
455455
<argument name="data" xsi:type="array">
456456
<item name="js_config" xsi:type="array">
457457
<item name="component" xsi:type="string">Magento_Ui/js/grid/columns/date</item>
@@ -562,7 +562,7 @@
562562
</item>
563563
</argument>
564564
</column>
565-
<column name="gender" class="CustomerAttributeColumn">
565+
<column name="gender" class="Magento\Customer\Ui\Component\Listing\Column\AttributeColumn">
566566
<argument name="data" xsi:type="array">
567567
<item name="js_config" xsi:type="array">
568568
<item name="component" xsi:type="string">Magento_Ui/js/grid/columns/column</item>

0 commit comments

Comments
 (0)