3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Customer \Model ;
7
9
8
10
use Magento \Customer \Api \Data \OptionInterfaceFactory ;
9
11
use Magento \Customer \Api \Data \ValidationRuleInterfaceFactory ;
10
12
use Magento \Customer \Api \Data \AttributeMetadataInterfaceFactory ;
11
13
use Magento \Eav \Api \Data \AttributeDefaultValueInterface ;
14
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
15
+ use Magento \Framework \App \ObjectManager ;
12
16
13
17
/**
14
18
* Converter for AttributeMetadata
15
19
*/
16
20
class AttributeMetadataConverter
17
21
{
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
+
18
36
/**
19
37
* @var OptionInterfaceFactory
20
38
*/
@@ -35,24 +53,32 @@ class AttributeMetadataConverter
35
53
*/
36
54
protected $ dataObjectHelper ;
37
55
56
+ /**
57
+ * @var ScopeConfigInterface
58
+ */
59
+ private $ scopeConfig ;
60
+
38
61
/**
39
62
* Initialize the Converter
40
63
*
41
64
* @param OptionInterfaceFactory $optionFactory
42
65
* @param ValidationRuleInterfaceFactory $validationRuleFactory
43
66
* @param AttributeMetadataInterfaceFactory $attributeMetadataFactory
44
67
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
68
+ * @param ScopeConfigInterface $scopeConfig
45
69
*/
46
70
public function __construct (
47
71
OptionInterfaceFactory $ optionFactory ,
48
72
ValidationRuleInterfaceFactory $ validationRuleFactory ,
49
73
AttributeMetadataInterfaceFactory $ attributeMetadataFactory ,
50
- \Magento \Framework \Api \DataObjectHelper $ dataObjectHelper
74
+ \Magento \Framework \Api \DataObjectHelper $ dataObjectHelper ,
75
+ ScopeConfigInterface $ scopeConfig = null
51
76
) {
52
77
$ this ->optionFactory = $ optionFactory ;
53
78
$ this ->validationRuleFactory = $ validationRuleFactory ;
54
79
$ this ->attributeMetadataFactory = $ attributeMetadataFactory ;
55
80
$ this ->dataObjectHelper = $ dataObjectHelper ;
81
+ $ this ->scopeConfig = $ scopeConfig ?? ObjectManager::getInstance ()->get (ScopeConfigInterface::class);
56
82
}
57
83
58
84
/**
@@ -64,28 +90,34 @@ public function __construct(
64
90
public function createMetadataAttribute ($ attribute )
65
91
{
66
92
$ 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 );
82
114
}
83
- $ optionDataObject ->setOptions ($ optionArray );
115
+ $ optionDataObject ->setLabel ($ option ['label ' ]);
116
+ $ options [] = $ optionDataObject ;
84
117
}
85
- $ optionDataObject ->setLabel ($ option ['label ' ]);
86
- $ options [] = $ optionDataObject ;
87
118
}
88
119
}
120
+
89
121
$ validationRules = [];
90
122
foreach ((array )$ attribute ->getValidateRules () as $ name => $ value ) {
91
123
$ validationRule = $ this ->validationRuleFactory ->create ()
@@ -122,4 +154,26 @@ public function createMetadataAttribute($attribute)
122
154
->setIsFilterableInGrid ($ attribute ->getIsFilterableInGrid ())
123
155
->setIsSearchableInGrid ($ attribute ->getIsSearchableInGrid ());
124
156
}
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
+ }
125
179
}
0 commit comments