Skip to content

Commit f9d7168

Browse files
authored
Merge pull request #8238 from magento-lynx/eav-graphql
[LYNX] Delivery EAV-GraphQL
2 parents e503cee + b570647 commit f9d7168

File tree

54 files changed

+2961
-234
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2961
-234
lines changed

app/code/Magento/Customer/Model/ResourceModel/CustomerRepository.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
namespace Magento\Customer\Model\ResourceModel;
87

98
use Magento\Customer\Api\CustomerMetadataInterface;
@@ -407,8 +406,8 @@ public function getById($customerId)
407406
* Retrieve customers which match a specified criteria.
408407
*
409408
* This call returns an array of objects, but detailed information about each object’s attributes might not be
410-
* included. See https://developer.adobe.com/commerce/webapi/rest/attributes#CustomerRepositoryInterface to determine
411-
* which call to use to get detailed information about all attributes for an object.
409+
* included. See https://developer.adobe.com/commerce/webapi/rest/attributes#CustomerRepositoryInterface
410+
* to determine which call to use to get detailed information about all attributes for an object.
412411
*
413412
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
414413
* @return \Magento\Customer\Api\Data\CustomerSearchResultsInterface
@@ -540,6 +539,14 @@ private function prepareCustomerData(array $customerData): array
540539
{
541540
if (isset($customerData[CustomerInterface::CUSTOM_ATTRIBUTES])) {
542541
foreach ($customerData[CustomerInterface::CUSTOM_ATTRIBUTES] as $attribute) {
542+
if (empty($attribute['value'])
543+
&& !empty($attribute['selected_options'])
544+
&& is_array($attribute['selected_options'])
545+
) {
546+
$attribute['value'] = implode(',', array_map(function ($option): string {
547+
return $option['value'] ?? '';
548+
}, $attribute['selected_options']));
549+
}
543550
$customerData[$attribute['attribute_code']] = $attribute['value'];
544551
}
545552
unset($customerData[CustomerInterface::CUSTOM_ATTRIBUTES]);

app/code/Magento/Customer/Test/Fixture/CustomerAttribute.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ class CustomerAttribute implements RevertibleDataFixtureInterface
3636
'is_unique' => '0',
3737
'frontend_class' => null,
3838
'used_in_forms' => [],
39+
'sort_order' => 0,
40+
'attribute_set_id' => null,
41+
'attribute_group_id' => null,
42+
'input_filter' => null,
43+
'multiline_count' => 0,
44+
'validate_rules' => null
3945
];
4046

4147
/**

app/code/Magento/CustomerGraphQl/Model/Customer/Address/ExtractCustomerAddressData.php

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77

88
namespace Magento\CustomerGraphQl\Model\Customer\Address;
99

10+
use Magento\Customer\Api\AddressMetadataInterface;
11+
use Magento\Customer\Api\AddressRepositoryInterface;
1012
use Magento\Customer\Api\Data\AddressInterface;
1113
use Magento\Customer\Api\Data\CustomerInterface;
12-
use Magento\Framework\Api\CustomAttributesDataInterface;
13-
use Magento\Customer\Api\AddressRepositoryInterface;
14-
use Magento\Customer\Model\ResourceModel\Customer as CustomerResourceModel;
1514
use Magento\Customer\Model\CustomerFactory;
16-
use Magento\Framework\Webapi\ServiceOutputProcessor;
15+
use Magento\Customer\Model\ResourceModel\Customer as CustomerResourceModel;
16+
use Magento\EavGraphQl\Model\Output\Value\GetAttributeValueInterface;
17+
use Magento\Framework\Api\CustomAttributesDataInterface;
1718
use Magento\Framework\Serialize\SerializerInterface;
19+
use Magento\Framework\Webapi\ServiceOutputProcessor;
1820

1921
/**
2022
* Transform single customer address data from object to in array format
@@ -41,22 +43,30 @@ class ExtractCustomerAddressData
4143
*/
4244
private $customerFactory;
4345

46+
/**
47+
* @var GetAttributeValueInterface
48+
*/
49+
private GetAttributeValueInterface $getAttributeValue;
50+
4451
/**
4552
* @param ServiceOutputProcessor $serviceOutputProcessor
4653
* @param SerializerInterface $jsonSerializer
4754
* @param CustomerResourceModel $customerResourceModel
4855
* @param CustomerFactory $customerFactory
56+
* @param GetAttributeValueInterface $getAttributeValue
4957
*/
5058
public function __construct(
5159
ServiceOutputProcessor $serviceOutputProcessor,
5260
SerializerInterface $jsonSerializer,
5361
CustomerResourceModel $customerResourceModel,
54-
CustomerFactory $customerFactory
62+
CustomerFactory $customerFactory,
63+
GetAttributeValueInterface $getAttributeValue
5564
) {
5665
$this->serviceOutputProcessor = $serviceOutputProcessor;
5766
$this->jsonSerializer = $jsonSerializer;
5867
$this->customerResourceModel = $customerResourceModel;
5968
$this->customerFactory = $customerFactory;
69+
$this->getAttributeValue = $getAttributeValue;
6070
}
6171

6272
/**
@@ -100,31 +110,11 @@ public function execute(AddressInterface $address): array
100110
$addressData[CustomAttributesDataInterface::EXTENSION_ATTRIBUTES_KEY]
101111
);
102112
}
103-
$customAttributes = [];
104-
if (isset($addressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES])) {
105-
foreach ($addressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES] as $attribute) {
106-
$isArray = false;
107-
if (is_array($attribute['value'])) {
108-
// @ignoreCoverageStart
109-
$isArray = true;
110-
foreach ($attribute['value'] as $attributeValue) {
111-
if (is_array($attributeValue)) {
112-
$customAttributes[$attribute['attribute_code']] = $this->jsonSerializer->serialize(
113-
$attribute['value']
114-
);
115-
continue;
116-
}
117-
$customAttributes[$attribute['attribute_code']] = implode(',', $attribute['value']);
118-
continue;
119-
}
120-
// @ignoreCoverageEnd
121-
}
122-
if ($isArray) {
123-
continue;
124-
}
125-
$customAttributes[$attribute['attribute_code']] = $attribute['value'];
126-
}
127-
}
113+
114+
$customAttributes = isset($addressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES])
115+
? $this->formatCustomAttributes($addressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES])
116+
: ['custom_attributesV2' => []];
117+
128118
$addressData = array_merge($addressData, $customAttributes);
129119

130120
$addressData['customer_id'] = null;
@@ -135,4 +125,49 @@ public function execute(AddressInterface $address): array
135125

136126
return $addressData;
137127
}
128+
129+
/**
130+
* Retrieve formatted custom attributes
131+
*
132+
* @param array $attributes
133+
* @return array
134+
*/
135+
private function formatCustomAttributes(array $attributes)
136+
{
137+
foreach ($attributes as $attribute) {
138+
$isArray = false;
139+
if (is_array($attribute['value'])) {
140+
// @ignoreCoverageStart
141+
$isArray = true;
142+
foreach ($attribute['value'] as $attributeValue) {
143+
if (is_array($attributeValue)) {
144+
$customAttributes[$attribute['attribute_code']] = $this->jsonSerializer->serialize(
145+
$attribute['value']
146+
);
147+
continue;
148+
}
149+
$customAttributes[$attribute['attribute_code']] = implode(',', $attribute['value']);
150+
continue;
151+
}
152+
// @ignoreCoverageEnd
153+
}
154+
if ($isArray) {
155+
continue;
156+
}
157+
$customAttributes[$attribute['attribute_code']] = $attribute['value'];
158+
}
159+
160+
$customAttributes['custom_attributesV2'] = array_map(
161+
function (array $customAttribute) {
162+
return $this->getAttributeValue->execute(
163+
AddressMetadataInterface::ENTITY_TYPE_ADDRESS,
164+
$customAttribute['attribute_code'],
165+
$customAttribute['value']
166+
);
167+
},
168+
$attributes
169+
);
170+
171+
return $customAttributes;
172+
}
138173
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ private function createAccount(array $data, StoreInterface $store): CustomerInte
122122
$customerDataObject,
123123
CustomerInterface::class
124124
);
125+
125126
$data = array_merge($requiredDataAttributes, $data);
126127
$this->validateCustomerData->execute($data);
127128
$this->dataObjectHelper->populateWithArray(

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

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77

88
namespace Magento\CustomerGraphQl\Model\Customer;
99

10+
use Magento\Customer\Api\CustomerMetadataInterface;
1011
use Magento\Customer\Api\CustomerRepositoryInterface;
12+
use Magento\Customer\Api\Data\CustomerInterface;
13+
use Magento\EavGraphQl\Model\GetAttributeValueComposite;
1114
use Magento\Framework\Exception\LocalizedException;
12-
use Magento\Framework\Serialize\SerializerInterface;
1315
use Magento\Framework\Webapi\ServiceOutputProcessor;
14-
use Magento\Customer\Api\Data\CustomerInterface;
1516

1617
/**
1718
* Transform single customer data from object to in array format
@@ -24,20 +25,20 @@ class ExtractCustomerData
2425
private $serviceOutputProcessor;
2526

2627
/**
27-
* @var SerializerInterface
28+
* @var GetAttributeValueComposite
2829
*/
29-
private $serializer;
30+
private GetAttributeValueComposite $getAttributeValueComposite;
3031

3132
/**
3233
* @param ServiceOutputProcessor $serviceOutputProcessor
33-
* @param SerializerInterface $serializer
34+
* @param GetAttributeValueComposite $getAttributeValueComposite
3435
*/
3536
public function __construct(
3637
ServiceOutputProcessor $serviceOutputProcessor,
37-
SerializerInterface $serializer
38+
GetAttributeValueComposite $getAttributeValueComposite
3839
) {
3940
$this->serviceOutputProcessor = $serviceOutputProcessor;
40-
$this->serializer = $serializer;
41+
$this->getAttributeValueComposite = $getAttributeValueComposite;
4142
}
4243

4344
/**
@@ -77,30 +78,19 @@ public function execute(CustomerInterface $customer): array
7778
if (isset($customerData['extension_attributes'])) {
7879
$customerData = array_merge($customerData, $customerData['extension_attributes']);
7980
}
80-
$customAttributes = [];
8181
if (isset($customerData['custom_attributes'])) {
82-
foreach ($customerData['custom_attributes'] as $attribute) {
83-
$isArray = false;
84-
if (is_array($attribute['value'])) {
85-
$isArray = true;
86-
foreach ($attribute['value'] as $attributeValue) {
87-
if (is_array($attributeValue)) {
88-
$customAttributes[$attribute['attribute_code']] = $this->serializer->serialize(
89-
$attribute['value']
90-
);
91-
continue;
92-
}
93-
$customAttributes[$attribute['attribute_code']] = implode(',', $attribute['value']);
94-
continue;
95-
}
96-
}
97-
if ($isArray) {
98-
continue;
99-
}
100-
$customAttributes[$attribute['attribute_code']] = $attribute['value'];
101-
}
82+
$customerData['custom_attributes'] = array_map(
83+
function (array $customAttribute) {
84+
return $this->getAttributeValueComposite->execute(
85+
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
86+
$customAttribute
87+
);
88+
},
89+
$customerData['custom_attributes']
90+
);
91+
} else {
92+
$customerData['custom_attributes'] = [];
10293
}
103-
$customerData = array_merge($customerData, $customAttributes);
10494
//Fields are deprecated and should not be exposed on storefront.
10595
$customerData['group_id'] = null;
10696
$customerData['id'] = null;

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use Magento\Customer\Api\MetadataInterface;
1111
use Magento\EavGraphQl\Model\GetAttributesFormInterface;
12-
use Magento\EavGraphQl\Model\Uid;
1312

1413
/**
1514
* Attributes form provider for customer
@@ -21,25 +20,18 @@ class GetAttributesForm implements GetAttributesFormInterface
2120
*/
2221
private MetadataInterface $entity;
2322

24-
/**
25-
* @var Uid
26-
*/
27-
private Uid $uid;
28-
2923
/**
3024
* @var string
3125
*/
3226
private string $type;
3327

3428
/**
3529
* @param MetadataInterface $metadata
36-
* @param Uid $uid
3730
* @param string $type
3831
*/
39-
public function __construct(MetadataInterface $metadata, Uid $uid, string $type)
32+
public function __construct(MetadataInterface $metadata, string $type)
4033
{
4134
$this->entity = $metadata;
42-
$this->uid = $uid;
4335
$this->type = $type;
4436
}
4537

@@ -50,7 +42,7 @@ public function execute(string $formCode): ?array
5042
{
5143
$attributes = [];
5244
foreach ($this->entity->getAttributes($formCode) as $attribute) {
53-
$attributes[] = $this->uid->encode($this->type, $attribute->getAttributeCode());
45+
$attributes[] = ['entity_type' => $this->type, 'attribute_code' => $attribute->getAttributeCode()];
5446
}
5547
return $attributes;
5648
}

0 commit comments

Comments
 (0)