Skip to content

Commit 72ef5ed

Browse files
Lynx-307: country_id and region_id custom attributes (#185)
Co-authored-by: Sergii Ivashchenko <[email protected]>
1 parent 6a5060a commit 72ef5ed

File tree

5 files changed

+64
-45
lines changed

5 files changed

+64
-45
lines changed

app/code/Magento/CustomerGraphQl/Model/Customer/GetAttributesForm.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ public function execute(string $formCode): ?array
4242
{
4343
$attributes = [];
4444
foreach ($this->entity->getAttributes($formCode) as $attribute) {
45-
// region_id and country_id returns large datasets that is also not related between each other and
46-
// not filterable. DirectoryGraphQl contains queries that allow to retrieve this information in a
47-
// meaningful way
48-
if ($attribute->getAttributeCode() === 'region_id' || $attribute->getAttributeCode() === 'country_id') {
49-
continue;
50-
}
5145
$attributes[] = ['entity_type' => $this->type, 'attribute_code' => $attribute->getAttributeCode()];
5246
}
5347
return $attributes;

app/code/Magento/EavGraphQl/Model/Output/GetAttributeData.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<?php
2-
32
/**
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
7-
86
declare(strict_types=1);
97

108
namespace Magento\EavGraphQl\Model\Output;
@@ -24,12 +22,19 @@ class GetAttributeData implements GetAttributeDataInterface
2422
*/
2523
private EnumLookup $enumLookup;
2624

25+
/**
26+
* @var array
27+
*/
28+
private array $skipOptionsForAttributeCodes;
29+
2730
/**
2831
* @param EnumLookup $enumLookup
32+
* @param array $skipOptionsForAttributeCodes
2933
*/
30-
public function __construct(EnumLookup $enumLookup)
34+
public function __construct(EnumLookup $enumLookup, array $skipOptionsForAttributeCodes = [])
3135
{
3236
$this->enumLookup = $enumLookup;
37+
$this->skipOptionsForAttributeCodes = $skipOptionsForAttributeCodes;
3338
}
3439

3540
/**
@@ -91,7 +96,7 @@ private function getFrontendInput(AttributeInterface $attribute): string
9196
*/
9297
private function getOptions(AttributeInterface $attribute): array
9398
{
94-
if (!$attribute->getOptions()) {
99+
if (!$attribute->getOptions() || $this->skipOptions($attribute)) {
95100
return [];
96101
}
97102
return array_filter(
@@ -133,4 +138,18 @@ private function isDefault(mixed $value, mixed $defaultValue): bool
133138

134139
return in_array($value, explode(',', $defaultValue));
135140
}
141+
142+
/**
143+
* Skip attributes options for region_id and country_id
144+
*
145+
* Attributes region_id and country_id returns large datasets that is also not related between each other and
146+
* not filterable. DirectoryGraphQl contains queries that allow to retrieve this information in a meaningful way
147+
*
148+
* @param AttributeInterface $attribute
149+
* @return bool
150+
*/
151+
private function skipOptions(AttributeInterface $attribute): bool
152+
{
153+
return in_array($attribute->getAttributeCode(), $this->skipOptionsForAttributeCodes);
154+
}
136155
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,12 @@
99
<type name="Magento\Eav\Model\Entity\Attribute">
1010
<plugin name="entityAttributeChangePlugin" type="Magento\EavGraphQl\Plugin\Eav\AttributePlugin" />
1111
</type>
12+
<type name="Magento\EavGraphQl\Model\Output\GetAttributeData">
13+
<arguments>
14+
<argument name="skipOptionsForAttributeCodes" xsi:type="array">
15+
<item name="region_id" xsi:type="string">region_id</item>
16+
<item name="country_id" xsi:type="string">country_id</item>
17+
</argument>
18+
</arguments>
19+
</type>
1220
</config>

app/code/Magento/EavGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type Query {
1111
customAttributeMetadataV2(attributes: [AttributeInput!]): AttributesMetadataOutput! @resolver(class: "Magento\\EavGraphQl\\Model\\Resolver\\AttributesMetadata") @doc(description: "Retrieve EAV attributes metadata.") @cache(cacheIdentity: "Magento\\EavGraphQl\\Model\\Resolver\\Cache\\CustomAttributeMetadataV2Identity")
1212
attributesForm(formCode: String! @doc(description: "Form code.")): AttributesFormOutput!
1313
@resolver(class: "Magento\\EavGraphQl\\Model\\Resolver\\AttributesForm")
14-
@doc(description: "Retrieve EAV attributes associated to a frontend form. For region_id and country_id attributes information use DirectoryGraphQl module.")
14+
@doc(description: "Retrieve EAV attributes associated to a frontend form. Use countries query provided by DirectoryGraphQl module to retrieve region_id and country_id attribute options.")
1515
@cache(cacheIdentity: "Magento\\Eav\\Model\\Cache\\AttributesFormIdentity")
1616
attributesList(
1717
entityType: AttributeEntityTypeEnum! @doc(description: "Entity type.")

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/AttributesFormTest.php

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,6 @@ class AttributesFormTest extends GraphQlAbstract
5959
'used_in_forms' => ['customer_address_edit']
6060
],
6161
'attribute_2'
62-
),
63-
DataFixture(
64-
CustomerAttribute::class,
65-
[
66-
'entity_type_id' => AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS,
67-
'used_in_forms' => ['customer_register_address']
68-
],
69-
'attribute_3'
7062
)
7163
]
7264
public function testAttributesForm(): void
@@ -75,22 +67,16 @@ public function testAttributesForm(): void
7567
$attribute1 = DataFixtureStorageManager::getStorage()->get('attribute_1');
7668
/** @var AttributeInterface $attribute2 */
7769
$attribute2 = DataFixtureStorageManager::getStorage()->get('attribute_2');
78-
/** @var AttributeInterface $attribute3 */
79-
$attribute3 = DataFixtureStorageManager::getStorage()->get('attribute_3');
80-
$attribute3->setIsVisible(false)->save();
8170

8271
$result = $this->graphQlQuery(sprintf(self::QUERY, 'customer_register_address'));
8372

84-
foreach ($result['attributesForm']['items'] as $item) {
85-
if (array_contains($item, $attribute1->getAttributeCode())) {
86-
return;
87-
}
88-
$this->assertNotContains($attribute2->getAttributeCode(), $item);
89-
$this->assertNotContains($attribute3->getAttributeCode(), $item);
90-
$this->assertNotContains('region_id', $item);
91-
$this->assertNotContains('country_id', $item);
92-
}
93-
$this->fail(sprintf("Attribute '%s' not found in query response", $attribute1->getAttributeCode()));
73+
$this->assertNotEmpty($result['attributesForm']['items']);
74+
$codes = $this->getAttributeCodes($result['attributesForm']['items']);
75+
76+
$this->assertContains($attribute1->getAttributeCode(), $codes);
77+
$this->assertContains('country_id', $codes);
78+
$this->assertContains('region_id', $codes);
79+
$this->assertNotContains($attribute2->getAttributeCode(), $codes);
9480
}
9581

9682
public function testAttributesFormAdminHtmlForm(): void
@@ -152,13 +138,10 @@ public function testAttributesFormScope(): void
152138

153139
$result = $this->graphQlQuery(sprintf(self::QUERY, 'customer_register_address'));
154140

155-
foreach ($result['attributesForm']['items'] as $item) {
156-
if (array_contains($item, $attribute1->getAttributeCode())) {
157-
$this->fail(
158-
sprintf("Attribute '%s' found in query response in global scope", $attribute1->getAttributeCode())
159-
);
160-
}
161-
}
141+
$this->assertNotEmpty($result['attributesForm']['items']);
142+
$codes = $this->getAttributeCodes($result['attributesForm']['items']);
143+
144+
$this->assertNotContains($attribute1->getAttributeCode(), $codes);
162145

163146
/** @var StoreInterface $store */
164147
$store = DataFixtureStorageManager::getStorage()->get('store2');
@@ -170,16 +153,31 @@ public function testAttributesFormScope(): void
170153
['Store' => $store->getCode()]
171154
);
172155

173-
foreach ($result['attributesForm']['items'] as $item) {
174-
if (array_contains($item, $attribute1->getAttributeCode())) {
175-
return;
176-
}
177-
}
178-
$this->fail(
156+
$this->assertNotEmpty($result['attributesForm']['items']);
157+
$codes = $this->getAttributeCodes($result['attributesForm']['items']);
158+
$this->assertContains(
159+
$attribute1->getAttributeCode(),
160+
$codes,
179161
sprintf(
180162
"Attribute '%s' not found in query response in website scope",
181163
$attribute1->getAttributeCode()
182164
)
183165
);
184166
}
167+
168+
/**
169+
* Retrieve an array of attribute codes based on an array of attributes data
170+
*
171+
* @param array $attributes
172+
* @return array
173+
*/
174+
private function getAttributeCodes(array $attributes): array
175+
{
176+
return array_map(
177+
function (array $attribute) {
178+
return $attribute['code'];
179+
},
180+
$attributes
181+
);
182+
}
185183
}

0 commit comments

Comments
 (0)