5
5
*/
6
6
namespace Magento \Customer \Block \Address ;
7
7
8
+ use Magento \Framework \Exception \LocalizedException ;
8
9
use Magento \Framework \Exception \NoSuchEntityException ;
9
10
10
11
/**
@@ -46,6 +47,11 @@ class Edit extends \Magento\Directory\Block\Data
46
47
*/
47
48
protected $ dataObjectHelper ;
48
49
50
+ /**
51
+ * @var \Magento\Customer\Api\AddressMetadataInterface
52
+ */
53
+ private $ addressMetadata ;
54
+
49
55
/**
50
56
* Constructor
51
57
*
@@ -61,6 +67,7 @@ class Edit extends \Magento\Directory\Block\Data
61
67
* @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
62
68
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
63
69
* @param array $data
70
+ * @param \Magento\Customer\Api\AddressMetadataInterface|null $addressMetadata
64
71
*
65
72
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
66
73
*/
@@ -76,13 +83,15 @@ public function __construct(
76
83
\Magento \Customer \Api \Data \AddressInterfaceFactory $ addressDataFactory ,
77
84
\Magento \Customer \Helper \Session \CurrentCustomer $ currentCustomer ,
78
85
\Magento \Framework \Api \DataObjectHelper $ dataObjectHelper ,
79
- array $ data = []
86
+ array $ data = [],
87
+ \Magento \Customer \Api \AddressMetadataInterface $ addressMetadata = null
80
88
) {
81
89
$ this ->_customerSession = $ customerSession ;
82
90
$ this ->_addressRepository = $ addressRepository ;
83
91
$ this ->addressDataFactory = $ addressDataFactory ;
84
92
$ this ->currentCustomer = $ currentCustomer ;
85
93
$ this ->dataObjectHelper = $ dataObjectHelper ;
94
+ $ this ->addressMetadata = $ addressMetadata ;
86
95
parent ::__construct (
87
96
$ context ,
88
97
$ directoryHelper ,
@@ -103,6 +112,32 @@ protected function _prepareLayout()
103
112
{
104
113
parent ::_prepareLayout ();
105
114
115
+ $ this ->initAddressObject ();
116
+
117
+ $ this ->pageConfig ->getTitle ()->set ($ this ->getTitle ());
118
+
119
+ if ($ postedData = $ this ->_customerSession ->getAddressFormData (true )) {
120
+ $ postedData ['region ' ] = [
121
+ 'region_id ' => isset ($ postedData ['region_id ' ]) ? $ postedData ['region_id ' ] : null ,
122
+ 'region ' => $ postedData ['region ' ],
123
+ ];
124
+ $ this ->dataObjectHelper ->populateWithArray (
125
+ $ this ->_address ,
126
+ $ postedData ,
127
+ \Magento \Customer \Api \Data \AddressInterface::class
128
+ );
129
+ }
130
+ $ this ->precheckRequiredAttributes ();
131
+ return $ this ;
132
+ }
133
+
134
+ /**
135
+ * Initialize address object.
136
+ *
137
+ * @return void
138
+ */
139
+ private function initAddressObject ()
140
+ {
106
141
// Init address object
107
142
if ($ addressId = $ this ->getRequest ()->getParam ('id ' )) {
108
143
try {
@@ -124,22 +159,26 @@ protected function _prepareLayout()
124
159
$ this ->_address ->setLastname ($ customer ->getLastname ());
125
160
$ this ->_address ->setSuffix ($ customer ->getSuffix ());
126
161
}
162
+ }
127
163
128
- $ this ->pageConfig ->getTitle ()->set ($ this ->getTitle ());
129
-
130
- if ($ postedData = $ this ->_customerSession ->getAddressFormData (true )) {
131
- $ postedData ['region ' ] = [
132
- 'region_id ' => isset ($ postedData ['region_id ' ]) ? $ postedData ['region_id ' ] : null ,
133
- 'region ' => $ postedData ['region ' ],
134
- ];
135
- $ this ->dataObjectHelper ->populateWithArray (
136
- $ this ->_address ,
137
- $ postedData ,
138
- \Magento \Customer \Api \Data \AddressInterface::class
139
- );
164
+ /**
165
+ * Precheck attributes that may be required in attribute configuration.
166
+ *
167
+ * @return void
168
+ */
169
+ private function precheckRequiredAttributes ()
170
+ {
171
+ $ precheckAttributes = $ this ->getData ('check_attributes_on_render ' );
172
+ $ requiredAttributesPrechecked = [];
173
+ if (!empty ($ precheckAttributes ) && is_array ($ precheckAttributes )) {
174
+ foreach ($ precheckAttributes as $ attributeCode ) {
175
+ $ attributeMetadata = $ this ->addressMetadata ->getAttributeMetadata ($ attributeCode );
176
+ if ($ attributeMetadata && $ attributeMetadata ->isRequired ()) {
177
+ $ requiredAttributesPrechecked [$ attributeCode ] = $ attributeCode ;
178
+ }
179
+ }
140
180
}
141
-
142
- return $ this ;
181
+ $ this ->setData ('required_attributes_prechecked ' , $ requiredAttributesPrechecked );
143
182
}
144
183
145
184
/**
0 commit comments