Skip to content

Commit 6a027c6

Browse files
authored
LYNX-111: Customer address custom_attributesV2 field
1 parent e18edcc commit 6a027c6

File tree

11 files changed

+255
-66
lines changed

11 files changed

+255
-66
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

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

10+
use Magento\Customer\Api\AddressMetadataInterface;
1011
use Magento\Customer\Api\Data\AddressInterface;
1112
use Magento\Customer\Api\Data\CustomerInterface;
13+
use Magento\EavGraphQl\Model\GetAttributeValueComposite;
1214
use Magento\Framework\Api\CustomAttributesDataInterface;
1315
use Magento\Customer\Api\AddressRepositoryInterface;
1416
use Magento\Customer\Model\ResourceModel\Customer as CustomerResourceModel;
@@ -41,22 +43,30 @@ class ExtractCustomerAddressData
4143
*/
4244
private $customerFactory;
4345

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

6272
/**
@@ -124,6 +134,19 @@ public function execute(AddressInterface $address): array
124134
}
125135
$customAttributes[$attribute['attribute_code']] = $attribute['value'];
126136
}
137+
138+
$customAttributesV2 = array_map(
139+
function (array $customAttribute) {
140+
return $this->getAttributeValueComposite->execute(
141+
AddressMetadataInterface::ENTITY_TYPE_ADDRESS,
142+
$customAttribute
143+
);
144+
},
145+
$addressData[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES]
146+
);
147+
$customAttributes['custom_attributesV2'] = $customAttributesV2;
148+
} else {
149+
$customAttributes['custom_attributesV2'] = [];
127150
}
128151
$addressData = array_merge($addressData, $customAttributes);
129152

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

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@
1010
use Magento\Customer\Api\CustomerMetadataInterface;
1111
use Magento\Customer\Api\CustomerRepositoryInterface;
1212
use Magento\Customer\Api\Data\CustomerInterface;
13-
use Magento\Eav\Model\AttributeRepository;
1413
use Magento\EavGraphQl\Model\GetAttributeValueComposite;
15-
use Magento\EavGraphQl\Model\Uid;
1614
use Magento\Framework\Exception\LocalizedException;
17-
use Magento\Framework\Exception\NoSuchEntityException;
1815
use Magento\Framework\Webapi\ServiceOutputProcessor;
1916

2017
/**
@@ -27,36 +24,20 @@ class ExtractCustomerData
2724
*/
2825
private $serviceOutputProcessor;
2926

30-
/**
31-
* @var AttributeRepository
32-
*/
33-
private AttributeRepository $attributeRepository;
34-
35-
/**
36-
* @var Uid
37-
*/
38-
private Uid $uid;
39-
4027
/**
4128
* @var GetAttributeValueComposite
4229
*/
4330
private GetAttributeValueComposite $getAttributeValueComposite;
4431

4532
/**
4633
* @param ServiceOutputProcessor $serviceOutputProcessor
47-
* @param AttributeRepository $attributeRepository
48-
* @param Uid $uid
4934
* @param GetAttributeValueComposite $getAttributeValueComposite
5035
*/
5136
public function __construct(
5237
ServiceOutputProcessor $serviceOutputProcessor,
53-
AttributeRepository $attributeRepository,
54-
Uid $uid,
5538
GetAttributeValueComposite $getAttributeValueComposite
5639
) {
5740
$this->serviceOutputProcessor = $serviceOutputProcessor;
58-
$this->attributeRepository = $attributeRepository;
59-
$this->uid = $uid;
6041
$this->getAttributeValueComposite = $getAttributeValueComposite;
6142
}
6243

@@ -100,10 +81,15 @@ public function execute(CustomerInterface $customer): array
10081
if (isset($customerData['custom_attributes'])) {
10182
$customerData['custom_attributes'] = array_map(
10283
function (array $customAttribute) {
103-
return $this->getAttributeValueComposite->execute($customAttribute);
84+
return $this->getAttributeValueComposite->execute(
85+
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
86+
$customAttribute
87+
);
10488
},
10589
$customerData['custom_attributes']
10690
);
91+
} else {
92+
$customerData['custom_attributes'] = [];
10793
}
10894
//Fields are deprecated and should not be exposed on storefront.
10995
$customerData['group_id'] = null;

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Magento\CustomerGraphQl\Model\Customer;
99

10-
use Magento\Customer\Api\CustomerMetadataInterface;
1110
use Magento\Eav\Model\AttributeRepository;
1211
use Magento\EavGraphQl\Model\GetAttributeSelectedOptionComposite;
1312
use Magento\EavGraphQl\Model\GetAttributeValueInterface;
@@ -59,23 +58,26 @@ public function __construct(
5958
/**
6059
* @inheritDoc
6160
*/
62-
public function execute(array $customAttribute): ?array
61+
public function execute(string $entityType, array $customAttribute): ?array
6362
{
6463
$attr = $this->attributeRepository->get(
65-
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
64+
$entityType,
6665
$customAttribute['attribute_code']
6766
);
6867

6968
$result = [
7069
'uid' => $this->uid->encode(
71-
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
70+
$entityType,
7271
$customAttribute['attribute_code']
7372
),
7473
'code' => $customAttribute['attribute_code']
7574
];
7675

7776
if (in_array($attr->getFrontendInput(), $this->frontendInputs)) {
78-
$result['selected_options'] = $this->attributeSelectedOptionComposite->execute($customAttribute);
77+
$result['selected_options'] = $this->attributeSelectedOptionComposite->execute(
78+
$entityType,
79+
$customAttribute
80+
);
7981
} else {
8082
$result['value'] = $customAttribute['value'];
8183
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Magento\CustomerGraphQl\Model\Customer;
99

10-
use Magento\Customer\Api\CustomerMetadataInterface;
1110
use Magento\Eav\Model\AttributeRepository;
1211
use Magento\EavGraphQl\Model\GetAttributeSelectedOptionInterface;
1312
use Magento\Framework\GraphQl\Query\Uid;
@@ -33,7 +32,7 @@ class GetCustomSelectedOptionAttributes implements GetAttributeSelectedOptionInt
3332
*/
3433
public function __construct(
3534
Uid $uid,
36-
AttributeRepository $attributeRepository,
35+
AttributeRepository $attributeRepository
3736
) {
3837
$this->uid = $uid;
3938
$this->attributeRepository = $attributeRepository;
@@ -42,10 +41,10 @@ public function __construct(
4241
/**
4342
* @inheritDoc
4443
*/
45-
public function execute(array $customAttribute): ?array
44+
public function execute(string $entityType, array $customAttribute): ?array
4645
{
4746
$attr = $this->attributeRepository->get(
48-
CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
47+
$entityType,
4948
$customAttribute['attribute_code']
5049
);
5150

app/code/Magento/CustomerGraphQl/etc/graphql/di.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
<arguments>
143143
<argument name="providers" xsi:type="array">
144144
<item name="customer" xsi:type="object">Magento\CustomerGraphQl\Model\Customer\GetCustomAttributes</item>
145+
<item name="customer_address" xsi:type="object">Magento\CustomerGraphQl\Model\Customer\GetCustomAttributes</item>
145146
</argument>
146147
</arguments>
147148
</type>
@@ -157,6 +158,7 @@
157158
<arguments>
158159
<argument name="providers" xsi:type="array">
159160
<item name="customer" xsi:type="object">Magento\CustomerGraphQl\Model\Customer\GetCustomSelectedOptionAttributes</item>
161+
<item name="customer_address" xsi:type="object">Magento\CustomerGraphQl\Model\Customer\GetCustomSelectedOptionAttributes</item>
160162
</argument>
161163
</arguments>
162164
</type>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ type CustomerAddress @doc(description: "Contains detailed information about a cu
160160
vat_id: String @doc(description: "The customer's Value-added tax (VAT) number (for corporate customers).")
161161
default_shipping: Boolean @doc(description: "Indicates whether the address is the customer's default shipping address.")
162162
default_billing: Boolean @doc(description: "Indicates whether the address is the customer's default billing address.")
163-
custom_attributes: [CustomerAddressAttribute] @deprecated(reason: "Custom attributes should not be put into a container.")
163+
custom_attributes: [CustomerAddressAttribute] @deprecated(reason: "Use custom_attributesV2 instead.")
164+
custom_attributesV2: [AttributeValueInterface!]! @doc(description: "Customer address' custom attributes.")
164165
extension_attributes: [CustomerAddressAttribute] @doc(description: "Contains any extension attributes for the address.")
165166
}
166167

app/code/Magento/EavGraphQl/Model/GetAttributeSelectedOptionComposite.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,25 @@ public function __construct(array $providers = [])
3131
/**
3232
* Returns right GetAttributeSelectedOptionInterface to use for attribute with $attributeCode
3333
*
34+
* @param string $entityType
3435
* @param array $customAttribute
3536
* @return array|null
3637
* @throws RuntimeException
3738
*/
38-
public function execute(array $customAttribute): ?array
39+
public function execute(string $entityType, array $customAttribute): ?array
3940
{
40-
foreach ($this->providers as $provider) {
41-
if (!$provider instanceof GetAttributeSelectedOptionInterface) {
42-
throw new RuntimeException(
43-
__('Configured attribute selected option data providers should implement
44-
GetAttributeSelectedOptionInterface')
45-
);
46-
}
47-
48-
try {
49-
return $provider->execute($customAttribute);
50-
} catch (LocalizedException $e) {
51-
continue;
52-
}
41+
if (!isset($this->providers[$entityType])) {
42+
throw new RuntimeException(
43+
__(sprintf('"%s" entity type not set in providers', $entityType))
44+
);
45+
}
46+
if (!$this->providers[$entityType] instanceof GetAttributeSelectedOptionInterface) {
47+
throw new RuntimeException(
48+
__('Configured attribute selected option data providers should implement
49+
GetAttributeSelectedOptionInterface')
50+
);
5351
}
54-
return null;
52+
53+
return $this->providers[$entityType]->execute($entityType, $customAttribute);
5554
}
5655
}

app/code/Magento/EavGraphQl/Model/GetAttributeSelectedOptionInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@
1010
use Magento\Framework\Exception\LocalizedException;
1111

1212
/**
13-
* Interface for getting custom attributes seelcted options.
13+
* Interface for getting custom attributes selected options.
1414
*/
1515
interface GetAttributeSelectedOptionInterface
1616
{
1717
/**
1818
* Retrieve all selected options of an attribute filtered by attribute code
1919
*
20+
* @param string $entityType
2021
* @param array $customAttribute
2122
* @return array|null
2223
* @throws LocalizedException
2324
*/
24-
public function execute(array $customAttribute): ?array;
25+
public function execute(string $entityType, array $customAttribute): ?array;
2526
}

app/code/Magento/EavGraphQl/Model/GetAttributeValueComposite.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,24 @@ public function __construct(array $providers = [])
3131
/**
3232
* Returns right GetAttributeValueInterface to use for attribute with $attributeCode
3333
*
34+
* @param string $entityType
3435
* @param array $customAttribute
3536
* @return array|null
3637
* @throws RuntimeException
3738
*/
38-
public function execute(array $customAttribute): ?array
39+
public function execute(string $entityType, array $customAttribute): ?array
3940
{
40-
foreach ($this->providers as $provider) {
41-
if (!$provider instanceof GetAttributeValueInterface) {
42-
throw new RuntimeException(
43-
__('Configured attribute data providers should implement GetAttributeValueInterface')
44-
);
45-
}
46-
47-
try {
48-
return $provider->execute($customAttribute);
49-
} catch (LocalizedException $e) {
50-
continue;
51-
}
41+
if (!isset($this->providers[$entityType])) {
42+
throw new RuntimeException(
43+
__(sprintf('"%s" entity type not set in providers', $entityType))
44+
);
45+
}
46+
if (!$this->providers[$entityType] instanceof GetAttributeValueInterface) {
47+
throw new RuntimeException(
48+
__('Configured attribute data providers should implement GetAttributeValueInterface')
49+
);
5250
}
53-
return null;
51+
52+
return $this->providers[$entityType]->execute($entityType, $customAttribute);
5453
}
5554
}

app/code/Magento/EavGraphQl/Model/GetAttributeValueInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ interface GetAttributeValueInterface
1717
/**
1818
* Retrieve all attributes filtered by attribute code
1919
*
20+
* @param string $entityType
2021
* @param array $customAttribute
2122
* @return array|null
2223
* @throws LocalizedException
2324
*/
24-
public function execute(array $customAttribute): ?array;
25+
public function execute(string $entityType, array $customAttribute): ?array;
2526
}

0 commit comments

Comments
 (0)