Skip to content

Commit 0effe9a

Browse files
author
Mikhaël Bois
authored
[PWA-2509] Store view specific label/localization support for product attributes (#17)
* PWA-1936 Replace sort option label with store label * PWA-1936 Fix deps
1 parent 06b364f commit 0effe9a

File tree

6 files changed

+163
-8
lines changed

6 files changed

+163
-8
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQlAux\Plugin\Model\Resolver\Category;
9+
10+
use Magento\Eav\Model\Config;
11+
use Magento\Framework\Exception\LocalizedException;
12+
13+
class SortFields
14+
{
15+
/**
16+
* @var Config
17+
*/
18+
private $eavConfig;
19+
20+
/**
21+
* @param Config $eavConfig
22+
*/
23+
public function __construct(Config $eavConfig)
24+
{
25+
$this->eavConfig = $eavConfig;
26+
}
27+
28+
/**
29+
* Replace sort option label with store label
30+
*
31+
* @param \Magento\CatalogGraphQl\Model\Resolver\Category\SortFields $subject
32+
* @param array $result
33+
* @return array
34+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
35+
* @throws LocalizedException
36+
*/
37+
public function afterResolve(
38+
\Magento\CatalogGraphQl\Model\Resolver\Category\SortFields $subject,
39+
array $result
40+
): array {
41+
if (isset($result['options']) && count($result['options']) > 0) {
42+
foreach ($result['options'] as &$option) {
43+
/** @var $attribute \Magento\Eav\Model\Entity\Attribute */
44+
$attribute = $this->eavConfig->getAttribute(
45+
\Magento\Catalog\Model\Product::ENTITY,
46+
$option['value']
47+
);
48+
49+
if ($attribute->getStoreLabel()) {
50+
$option['label'] = $attribute->getStoreLabel();
51+
}
52+
}
53+
}
54+
55+
return $result;
56+
}
57+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQlAux\Test\Unit\Plugin\Model\Resolver\Category;
9+
10+
use Magento\CatalogGraphQl\Model\Resolver\Category\SortFields;
11+
use Magento\CatalogGraphQlAux\Plugin\Model\Resolver\Category\SortFields as SortFieldsPlugin;
12+
use Magento\Eav\Model\Config;
13+
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
16+
17+
class SortFieldsTest extends TestCase
18+
{
19+
/**
20+
* @var SortFieldsPlugin
21+
*/
22+
protected $model;
23+
24+
/**
25+
* @var MockObject
26+
*/
27+
protected $eavConfigMock;
28+
29+
protected function setUp(): void
30+
{
31+
$this->eavConfigMock = $this->createMock(Config::class);
32+
33+
$this->model = new SortFieldsPlugin(
34+
$this->eavConfigMock
35+
);
36+
}
37+
38+
public function testAfterResolveWithEmptyResult()
39+
{
40+
$sortFieldsMock = $this->createMock(SortFields::class);
41+
42+
$resultMock = [];
43+
44+
$this->assertEquals(
45+
$resultMock,
46+
$this->model->afterResolve($sortFieldsMock, $resultMock)
47+
);
48+
}
49+
50+
public function testAfterResolveWithData()
51+
{
52+
$attributeValue = 'attribute_code';
53+
$attributeAdminLabel = 'Admin Label';
54+
$attributeStoreLabel = 'Store Label';
55+
56+
$attribute = $this->getMockBuilder(AbstractAttribute::class)
57+
->addMethods(['getStoreLabel'])
58+
->disableOriginalConstructor()
59+
->getMockForAbstractClass();
60+
$attribute->expects($this->any())
61+
->method('getStoreLabel')
62+
->willReturn($attributeStoreLabel);
63+
64+
$this->eavConfigMock->expects($this->atLeastOnce())
65+
->method('getAttribute')
66+
->willReturn($attribute);
67+
68+
$expectedResult = [
69+
'options' => [
70+
[
71+
'label' => $attributeStoreLabel,
72+
'value' => $attributeValue
73+
]
74+
]
75+
];
76+
77+
$sortFieldsMock = $this->createMock(SortFields::class);
78+
79+
$resultMock = [
80+
'options' => [
81+
[
82+
'label' => $attributeAdminLabel,
83+
'value' => $attributeValue
84+
]
85+
]
86+
];
87+
88+
$this->assertEquals(
89+
$expectedResult,
90+
$this->model->afterResolve($sortFieldsMock, $resultMock)
91+
);
92+
}
93+
}

CatalogGraphQlAux/etc/graphql/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
<type name="Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\Builder\Price">
2020
<plugin name="price-add-position" type="Magento\CatalogGraphQlAux\Plugin\Price"/>
2121
</type>
22+
<type name="Magento\CatalogGraphQl\Model\Resolver\Category\SortFields">
23+
<plugin name="sort-fields-label-by-store" type="Magento\CatalogGraphQlAux\Plugin\Model\Resolver\Category\SortFields"/>
24+
</type>
2225
<type name="Magento\CatalogGraphQlAux\Model\Resolver\CustomAttributes">
2326
<arguments>
2427
<argument name="selectableTypes" xsi:type="array">

NewsletterGraphQlPwa/composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
},
1212
"require": {
1313
"php": "~7.4.0||~8.1.0",
14-
"magento/framework": "*",
15-
"magento/module-store": "*",
16-
"magento/module-newsletter": "*"
14+
"magento/framework": "*"
1715
},
1816
"suggest": {
19-
"magento/module-graph-ql": "*"
17+
"magento/module-graph-ql": "*",
18+
"magento/module-newsletter": "*",
19+
"magento/module-store": "*"
2020
},
2121
"autoload": {
2222
"files": [

QuoteGraphQlPwa/composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
"require": {
1313
"php": "~7.4.0||~8.1.0",
1414
"magento/framework": "*",
15-
"magento/module-quote": "*",
16-
"magento/module-quote-graph-ql": "*"
15+
"magento/module-quote": "*"
1716
},
1817
"suggest": {
19-
"magento/module-graph-ql": "*"
18+
"magento/module-graph-ql": "*",
19+
"magento/module-quote-graph-ql": "*"
2020
},
2121
"autoload": {
2222
"files": [

WeeeGraphQlAux/composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"type": "magento2-module",
55
"require": {
66
"php": "~7.4.0||~8.1.0",
7-
"magento/framework": "*",
7+
"magento/framework": "*"
8+
},
9+
"suggest": {
810
"magento/module-eav-graph-ql-aux": "*"
911
},
1012
"license": [

0 commit comments

Comments
 (0)