55use Magento \Framework \View \Element \AbstractBlock ;
66use Magento \Checkout \Block \Checkout \LayoutProcessorInterface ;
77use Magento \Framework \View \Element \Template \Context ;
8+ use Magento \Framework \Exception \LocalizedException ;
89
910class LayoutProcessor extends AbstractBlock implements LayoutProcessorInterface
1011{
1112 protected $ scopeConfig ;
13+ protected $ jsLayout ;
1214
1315 /**
1416 * Constructor
@@ -32,22 +34,25 @@ public function __construct(Context $context, array $data = [])
3234 * @param mixed $jsLayout
3335 * @return array
3436 */
35- public function process ($ jsLayout )
37+ public function process ($ jsLayout ): array
3638 {
3739 $ moduleEnabled = $ this ->scopeConfig ->getValue ('postcodenl_api/general/enabled ' , \Magento \Store \Model \ScopeInterface::SCOPE_STORE );
3840
3941 if (!$ moduleEnabled ) {
4042 return $ jsLayout ;
4143 }
4244
43- // Shipping fields
44- $ shippingFields = &$ jsLayout ['components ' ]
45- ['checkout ' ]['children ' ]
46- ['steps ' ]['children ' ]
47- ['shipping-step ' ]['children ' ]
48- ['shippingAddress ' ]['children ' ]
49- ['shipping-address-fieldset ' ]['children ' ];
45+ $ this ->jsLayout = $ jsLayout ;
5046
47+ // Shipping fields
48+ $ shippingFields = &$ this ->_getJsLayoutRef ([
49+ 'components ' ,
50+ 'checkout ' , 'children ' ,
51+ 'steps ' , 'children ' ,
52+ 'shipping-step ' , 'children ' ,
53+ 'shippingAddress ' , 'children ' ,
54+ 'shipping-address-fieldset ' , 'children '
55+ ]);
5156 $ shippingFields = $ this ->_changeAddressFieldsPosition ($ shippingFields );
5257
5358 // Autofill fields copy
@@ -62,12 +67,18 @@ public function process($jsLayout)
6267 );
6368
6469 // Billing step
65- $ billingConfiguration = &$ jsLayout ['components ' ]
66- ['checkout ' ]['children ' ]
67- ['steps ' ]['children ' ]
68- ['billing-step ' ]['children ' ]
69- ['payment ' ]['children ' ]
70- ['payments-list ' ]['children ' ];
70+ try {
71+ $ billingConfiguration = &$ this ->_getJsLayoutRef ([
72+ 'components ' ,
73+ 'checkout ' , 'children ' ,
74+ 'steps ' , 'children ' ,
75+ 'billing-step ' , 'children ' ,
76+ 'payment ' , 'children ' ,
77+ 'payments-list ' , 'children ' ,
78+ ]);
79+ } catch (LocalizedException $ e ) {
80+
81+ }
7182
7283 if (isset ($ billingConfiguration )) {
7384 foreach ($ billingConfiguration as $ key => &$ billingForm ) {
@@ -88,14 +99,20 @@ public function process($jsLayout)
8899 }
89100
90101 // Billing address on payment page
91- $ billingFields = &$ jsLayout ['components ' ]
92- ['checkout ' ]['children ' ]
93- ['steps ' ]['children ' ]
94- ['billing-step ' ]['children ' ]
95- ['payment ' ]['children ' ]
96- ['afterMethods ' ]['children ' ]
97- ['billing-address-form ' ]['children ' ]
98- ['form-fields ' ]['children ' ];
102+ try {
103+ $ billingFields = &$ this ->_getJsLayoutRef ([
104+ 'components ' ,
105+ 'checkout ' , 'children ' ,
106+ 'steps ' , 'children ' ,
107+ 'billing-step ' , 'children ' ,
108+ 'payment ' , 'children ' ,
109+ 'afterMethods ' , 'children ' ,
110+ 'billing-address-form ' , 'children ' ,
111+ 'form-fields ' , 'children ' ,
112+ ]);
113+ } catch (LocalizedException $ e ) {
114+
115+ }
99116
100117 if (isset ($ billingFields )) {
101118 $ billingFields += $ this ->_updateCustomScope ($ autofillFields , 'billingAddressshared ' );
@@ -104,20 +121,47 @@ public function process($jsLayout)
104121 }
105122
106123 // Compatibility
107- $ magePlazaBillingFields = &$ jsLayout ['components ' ]
108- ['checkout ' ]['children ' ]
109- ['steps ' ]['children ' ]
110- ['shipping-step ' ]['children ' ]
111- ['billingAddress ' ]['children ' ]
112- ['billing-address-fieldset ' ]['children ' ];
124+ try {
125+ $ magePlazaBillingFields = &$ this ->_getJsLayoutRef ([
126+ 'components ' ,
127+ 'checkout ' , 'children ' ,
128+ 'steps ' , 'children ' ,
129+ 'shipping-step ' , 'children ' ,
130+ 'billingAddress ' , 'children ' ,
131+ 'billing-address-fieldset ' , 'children ' ,
132+ ]);
133+ } catch (LocalizedException $ e ) {
134+
135+ }
113136
114137 if (isset ($ magePlazaBillingFields )) {
115138 $ magePlazaBillingFields += $ this ->_updateCustomScope ($ autofillFields , 'billingAddress ' );
116139 $ magePlazaBillingFields = $ this ->_updateDataScope ($ magePlazaBillingFields , 'billingAddress ' );
117140 $ magePlazaBillingFields = $ this ->_changeAddressFieldsPosition ($ magePlazaBillingFields );
118141 }
119142
120- return $ jsLayout ;
143+ return $ this ->jsLayout ;
144+ }
145+
146+ /**
147+ * Get a reference to the specified path in $this->jsLayout.
148+ *
149+ * @param array $path - Path in Javascript layout.
150+ * @return array - Reference to path in $this->jsLayout.
151+ * @throws LocalizedException - Throw exception if path wasn't found.
152+ */
153+ private function &_getJsLayoutRef (array $ path ): array
154+ {
155+ $ result = &$ this ->jsLayout ;
156+ foreach ($ path as $ key ) {
157+ if (isset ($ result [$ key ])) {
158+ $ result = &$ result [$ key ];
159+ } else {
160+ throw new LocalizedException (__ ('Invalid path in Javascript layout: `%1` ' , implode ('. ' , $ path )));
161+ }
162+ }
163+
164+ return $ result ;
121165 }
122166
123167 /**
@@ -128,7 +172,7 @@ public function process($jsLayout)
128172 * @param string $dataScope
129173 * @return array - Fields with modified customScope.
130174 */
131- private function _updateCustomScope ($ fields , $ dataScope )
175+ private function _updateCustomScope ($ fields , $ dataScope ): array
132176 {
133177 foreach ($ fields as $ name => $ items ) {
134178 if (isset ($ items ['config ' ], $ items ['config ' ]['customScope ' ])) {
@@ -153,7 +197,7 @@ private function _updateCustomScope($fields, $dataScope)
153197 * @param string $dataScope
154198 * @return array - Fields with modified customScope.
155199 */
156- private function _updateDataScope ($ fields , $ dataScope )
200+ private function _updateDataScope ($ fields , $ dataScope ): array
157201 {
158202 foreach ($ fields as $ name => $ items ) {
159203 if (isset ($ items ['dataScope ' ])) {
@@ -175,7 +219,7 @@ private function _updateDataScope($fields, $dataScope)
175219 * @param array $addressFields
176220 * @return array
177221 */
178- private function _changeAddressFieldsPosition ($ addressFields )
222+ private function _changeAddressFieldsPosition ($ addressFields ): array
179223 {
180224 if ($ this ->scopeConfig ->getValue ('postcodenl_api/general/change_fields_position ' ) != '1 ' ) {
181225 return $ addressFields ;
0 commit comments