Skip to content

Commit 1da4f2a

Browse files
committed
Resolve API "V1/attributeMetadata/customerAddress/attribute/prefix" and "V1/attributeMetadata/customerAddress/attribute/suffix" can not get options issue24518
1 parent b5a38dc commit 1da4f2a

File tree

1 file changed

+73
-19
lines changed

1 file changed

+73
-19
lines changed

app/code/Magento/Customer/Model/AttributeMetadataConverter.php

Lines changed: 73 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,36 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Customer\Model;
79

810
use Magento\Customer\Api\Data\OptionInterfaceFactory;
911
use Magento\Customer\Api\Data\ValidationRuleInterfaceFactory;
1012
use Magento\Customer\Api\Data\AttributeMetadataInterfaceFactory;
1113
use Magento\Eav\Api\Data\AttributeDefaultValueInterface;
14+
use Magento\Framework\App\Config\ScopeConfigInterface;
15+
use Magento\Framework\App\ObjectManager;
1216

1317
/**
1418
* Converter for AttributeMetadata
1519
*/
1620
class AttributeMetadataConverter
1721
{
22+
/**
23+
* Attribute Code get options from system config
24+
*
25+
* @var array
26+
*/
27+
private const ATTRIBUTE_CODE_LIST_FROM_SYSTEM_CONFIG = ['prefix', 'suffix'];
28+
29+
/**
30+
* XML Path to get address config
31+
*
32+
* @var string
33+
*/
34+
private const XML_CUSTOMER_ADDRESS = 'customer/address/';
35+
1836
/**
1937
* @var OptionInterfaceFactory
2038
*/
@@ -35,24 +53,32 @@ class AttributeMetadataConverter
3553
*/
3654
protected $dataObjectHelper;
3755

56+
/**
57+
* @var ScopeConfigInterface
58+
*/
59+
private $scopeConfig;
60+
3861
/**
3962
* Initialize the Converter
4063
*
4164
* @param OptionInterfaceFactory $optionFactory
4265
* @param ValidationRuleInterfaceFactory $validationRuleFactory
4366
* @param AttributeMetadataInterfaceFactory $attributeMetadataFactory
4467
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
68+
* @param ScopeConfigInterface $scopeConfig
4569
*/
4670
public function __construct(
4771
OptionInterfaceFactory $optionFactory,
4872
ValidationRuleInterfaceFactory $validationRuleFactory,
4973
AttributeMetadataInterfaceFactory $attributeMetadataFactory,
50-
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper
74+
\Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
75+
ScopeConfigInterface $scopeConfig = null
5176
) {
5277
$this->optionFactory = $optionFactory;
5378
$this->validationRuleFactory = $validationRuleFactory;
5479
$this->attributeMetadataFactory = $attributeMetadataFactory;
5580
$this->dataObjectHelper = $dataObjectHelper;
81+
$this->scopeConfig = $scopeConfig ?? ObjectManager::getInstance()->get(ScopeConfigInterface::class);
5682
}
5783

5884
/**
@@ -64,28 +90,34 @@ public function __construct(
6490
public function createMetadataAttribute($attribute)
6591
{
6692
$options = [];
67-
if ($attribute->usesSource()) {
68-
foreach ($attribute->getSource()->getAllOptions() as $option) {
69-
$optionDataObject = $this->optionFactory->create();
70-
if (!is_array($option['value'])) {
71-
$optionDataObject->setValue($option['value']);
72-
} else {
73-
$optionArray = [];
74-
foreach ($option['value'] as $optionArrayValues) {
75-
$optionObject = $this->optionFactory->create();
76-
$this->dataObjectHelper->populateWithArray(
77-
$optionObject,
78-
$optionArrayValues,
79-
\Magento\Customer\Api\Data\OptionInterface::class
80-
);
81-
$optionArray[] = $optionObject;
93+
94+
if (in_array($attribute->getAttributeCode(), self::ATTRIBUTE_CODE_LIST_FROM_SYSTEM_CONFIG)) {
95+
$options = $this->getOptionFromConfig($attribute->getAttributeCode());
96+
} else {
97+
if ($attribute->usesSource()) {
98+
foreach ($attribute->getSource()->getAllOptions() as $option) {
99+
$optionDataObject = $this->optionFactory->create();
100+
if (!is_array($option['value'])) {
101+
$optionDataObject->setValue($option['value']);
102+
} else {
103+
$optionArray = [];
104+
foreach ($option['value'] as $optionArrayValues) {
105+
$optionObject = $this->optionFactory->create();
106+
$this->dataObjectHelper->populateWithArray(
107+
$optionObject,
108+
$optionArrayValues,
109+
\Magento\Customer\Api\Data\OptionInterface::class
110+
);
111+
$optionArray[] = $optionObject;
112+
}
113+
$optionDataObject->setOptions($optionArray);
82114
}
83-
$optionDataObject->setOptions($optionArray);
115+
$optionDataObject->setLabel($option['label']);
116+
$options[] = $optionDataObject;
84117
}
85-
$optionDataObject->setLabel($option['label']);
86-
$options[] = $optionDataObject;
87118
}
88119
}
120+
89121
$validationRules = [];
90122
foreach ((array)$attribute->getValidateRules() as $name => $value) {
91123
$validationRule = $this->validationRuleFactory->create()
@@ -122,4 +154,26 @@ public function createMetadataAttribute($attribute)
122154
->setIsFilterableInGrid($attribute->getIsFilterableInGrid())
123155
->setIsSearchableInGrid($attribute->getIsSearchableInGrid());
124156
}
157+
158+
/**
159+
* Get option from System Config instead of Use Source (Prefix, Suffix)
160+
*
161+
* @param string $attributeCode
162+
* @return \Magento\Customer\Api\Data\OptionInterface[]
163+
*/
164+
private function getOptionFromConfig($attributeCode)
165+
{
166+
$result = [];
167+
$value = $this->scopeConfig->getValue(self::XML_CUSTOMER_ADDRESS . $attributeCode . '_options');
168+
if ($value) {
169+
$optionArray = explode(';', $value);
170+
foreach ($optionArray as $value) {
171+
$optionObject = $this->optionFactory->create();
172+
$optionObject->setLabel($value);
173+
$optionObject->setValue($value);
174+
$result[] = $optionObject;
175+
}
176+
}
177+
return $result;
178+
}
125179
}

0 commit comments

Comments
 (0)