7
7
8
8
class LayoutProcessor implements \Magento \Checkout \Block \Checkout \LayoutProcessorInterface
9
9
{
10
+ /**
11
+ * Attributes with custom convertion to selects
12
+ * key is an attribute code
13
+ * value is a method to fetch values
14
+ *
15
+ * @var array
16
+ */
17
+ private $ attributesToConvert = [
18
+ 'prefix ' => 'getNamePrefixOptions ' ,
19
+ 'suffix ' => 'getNameSuffixOptions ' ,
20
+ ];
21
+
10
22
/**
11
23
* @var \Magento\Customer\Model\AttributeMetadataDataProvider
12
24
*/
@@ -22,28 +34,30 @@ class LayoutProcessor implements \Magento\Checkout\Block\Checkout\LayoutProcesso
22
34
*/
23
35
protected $ merger ;
24
36
37
+ /**
38
+ * @var \Magento\Customer\Model\Options
39
+ */
40
+ private $ options ;
41
+
25
42
/**
26
43
* @param \Magento\Customer\Model\AttributeMetadataDataProvider $attributeMetadataDataProvider
27
44
* @param \Magento\Ui\Component\Form\AttributeMapper $attributeMapper
45
+ * @param \Magento\Customer\Model\Options $options
28
46
* @param AttributeMerger $merger
29
47
*/
30
48
public function __construct (
31
49
\Magento \Customer \Model \AttributeMetadataDataProvider $ attributeMetadataDataProvider ,
32
50
\Magento \Ui \Component \Form \AttributeMapper $ attributeMapper ,
51
+ \Magento \Customer \Model \Options $ options ,
33
52
AttributeMerger $ merger
34
53
) {
35
54
$ this ->attributeMetadataDataProvider = $ attributeMetadataDataProvider ;
36
55
$ this ->attributeMapper = $ attributeMapper ;
56
+ $ this ->options = $ options ;
37
57
$ this ->merger = $ merger ;
38
58
}
39
59
40
- /**
41
- * Process js Layout of block
42
- *
43
- * @param array $jsLayout
44
- * @return array
45
- */
46
- public function process ($ jsLayout )
60
+ private function getElements ()
47
61
{
48
62
/** @var \Magento\Eav\Api\Data\AttributeInterface[] $attributes */
49
63
$ attributes = $ this ->attributeMetadataDataProvider ->loadAttributesCollection (
@@ -53,16 +67,61 @@ public function process($jsLayout)
53
67
54
68
$ elements = [];
55
69
foreach ($ attributes as $ attribute ) {
70
+ $ code = $ attribute ->getAttributeCode ();
56
71
if ($ attribute ->getIsUserDefined ()) {
57
72
continue ;
58
73
}
59
- $ elements [$ attribute -> getAttributeCode () ] = $ this ->attributeMapper ->map ($ attribute );
60
- if (isset ($ elements [$ attribute -> getAttributeCode () ]['label ' ])) {
61
- $ label = $ elements [$ attribute -> getAttributeCode () ]['label ' ];
62
- $ elements [$ attribute -> getAttributeCode () ]['label ' ] = __ ($ label );
74
+ $ elements [$ code ] = $ this ->attributeMapper ->map ($ attribute );
75
+ if (isset ($ elements [$ code ]['label ' ])) {
76
+ $ label = $ elements [$ code ]['label ' ];
77
+ $ elements [$ code ]['label ' ] = __ ($ label );
63
78
}
64
79
}
80
+ return $ elements ;
81
+ }
65
82
83
+ /**
84
+ * @param array $elements
85
+ * @return array
86
+ */
87
+ public function convertPrefixSuffix ($ elements )
88
+ {
89
+ $ codes = array_keys ($ this ->attributesToConvert );
90
+ foreach ($ elements as $ code => $ element ) {
91
+ if (!in_array ($ code , $ codes )) {
92
+ continue ;
93
+ }
94
+ $ options = call_user_func_array (
95
+ [$ this ->options , $ this ->attributesToConvert [$ code ]],
96
+ []
97
+ );
98
+ if (!is_array ($ options )) {
99
+ continue ;
100
+ }
101
+ $ elements [$ code ]['dataType ' ] = 'select ' ;
102
+ $ elements [$ code ]['formElement ' ] = 'select ' ;
103
+
104
+ foreach ($ options as $ key => $ value ) {
105
+ $ elements [$ code ]['options ' ][] = [
106
+ 'value ' => $ key ,
107
+ 'label ' => $ value ,
108
+ ];
109
+ }
110
+ }
111
+
112
+ return $ elements ;
113
+ }
114
+
115
+ /**
116
+ * Process js Layout of block
117
+ *
118
+ * @param array $jsLayout
119
+ * @return array
120
+ */
121
+ public function process ($ jsLayout )
122
+ {
123
+ $ elements = $ this ->getElements ();
124
+ $ elements = $ this ->convertPrefixSuffix ($ elements );
66
125
// The following code is a workaround for custom address attributes
67
126
if (isset ($ jsLayout ['components ' ]['checkout ' ]['children ' ]['steps ' ]['children ' ]['billing-step ' ]['children ' ]
68
127
['payment ' ]['children ' ]
0 commit comments