33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+
67namespace Magento \Customer \Controller \Adminhtml \Index ;
78
89use Magento \Backend \App \Action \Context ;
4041use Magento \Framework \Math \Random ;
4142use Magento \Framework \Reflection \DataObjectProcessor ;
4243use Magento \Framework \Registry ;
44+ use Magento \Framework \Validator \Exception ;
4345use Magento \Framework \View \Result \LayoutFactory ;
4446use Magento \Framework \View \Result \PageFactory ;
4547use Magento \Newsletter \Model \SubscriberFactory ;
@@ -243,10 +245,10 @@ protected function _extractData(
243245 /**
244246 * Saves default_billing and default_shipping flags for customer address
245247 *
246- * @deprecated 102.0.1 must be removed because addresses are save separately for now
247248 * @param array $addressIdList
248249 * @param array $extractedCustomerData
249250 * @return array
251+ * @deprecated 102.0.1 must be removed because addresses are save separately for now
250252 */
251253 protected function saveDefaultFlags (array $ addressIdList , array &$ extractedCustomerData )
252254 {
@@ -286,9 +288,9 @@ protected function saveDefaultFlags(array $addressIdList, array &$extractedCusto
286288 /**
287289 * Reformat customer addresses data to be compatible with customer service interface
288290 *
289- * @deprecated 102.0.1 addresses are saved separately for now
290291 * @param array $extractedCustomerData
291292 * @return array
293+ * @deprecated 102.0.1 addresses are saved separately for now
292294 */
293295 protected function _extractCustomerAddressData (array &$ extractedCustomerData )
294296 {
@@ -318,6 +320,7 @@ public function execute()
318320 {
319321 $ returnToEdit = false ;
320322 $ customerId = $ this ->getCurrentCustomerId ();
323+ $ customer = $ this ->customerDataFactory ->create ();
321324
322325 if ($ this ->getRequest ()->getPostValue ()) {
323326 try {
@@ -335,8 +338,6 @@ public function execute()
335338 $ customerData ['id ' ] = $ customerId ;
336339 }
337340
338- /** @var CustomerInterface $customer */
339- $ customer = $ this ->customerDataFactory ->create ();
340341 $ this ->dataObjectHelper ->populateWithArray (
341342 $ customer ,
342343 $ customerData ,
@@ -353,15 +354,14 @@ public function execute()
353354 try {
354355 $ this ->customerAccountManagement ->validateCustomerStoreIdByWebsiteId ($ customer );
355356 } catch (LocalizedException $ exception ) {
356- throw new LocalizedException (__ ("The Store View selected for sending Welcome email from " .
357+ throw new LocalizedException (__ ("The Store View selected for sending Welcome email from " .
357358 " is not related to the customer's associated website. " ));
358359 }
359360 }
360361
361362 // Save customer
362363 if ($ customerId ) {
363364 $ this ->_customerRepository ->save ($ customer );
364-
365365 $ this ->getEmailNotification ()->credentialsChanged ($ customer , $ currentCustomer ->getEmail ());
366366 } else {
367367 $ customer = $ this ->customerAccountManagement ->createAccount ($ customer );
@@ -386,13 +386,13 @@ public function execute()
386386 __ ('Something went wrong while saving the customer. ' )
387387 );
388388 $ returnToEdit = false ;
389- } catch (\ Magento \ Framework \ Validator \ Exception $ exception ) {
389+ } catch (Exception $ exception ) {
390390 $ messages = $ exception ->getMessages ();
391391 if (empty ($ messages )) {
392392 $ messages = $ exception ->getMessage ();
393393 }
394394 $ this ->_addSessionErrorMessages ($ messages );
395- $ this ->_getSession ()->setCustomerFormData ($ this ->retrieveFormattedFormData ());
395+ $ this ->_getSession ()->setCustomerFormData ($ this ->retrieveFormattedFormData ($ customer ));
396396 $ returnToEdit = true ;
397397 } catch (AbstractAggregateException $ exception ) {
398398 $ errors = $ exception ->getErrors ();
@@ -401,18 +401,18 @@ public function execute()
401401 $ messages [] = $ error ->getMessage ();
402402 }
403403 $ this ->_addSessionErrorMessages ($ messages );
404- $ this ->_getSession ()->setCustomerFormData ($ this ->retrieveFormattedFormData ());
404+ $ this ->_getSession ()->setCustomerFormData ($ this ->retrieveFormattedFormData ($ customer ));
405405 $ returnToEdit = true ;
406406 } catch (LocalizedException $ exception ) {
407407 $ this ->_addSessionErrorMessages ($ exception ->getMessage ());
408- $ this ->_getSession ()->setCustomerFormData ($ this ->retrieveFormattedFormData ());
408+ $ this ->_getSession ()->setCustomerFormData ($ this ->retrieveFormattedFormData ($ customer ));
409409 $ returnToEdit = true ;
410410 } catch (\Exception $ exception ) {
411411 $ this ->messageManager ->addExceptionMessage (
412412 $ exception ,
413413 __ ('Something went wrong while saving the customer. ' )
414414 );
415- $ this ->_getSession ()->setCustomerFormData ($ this ->retrieveFormattedFormData ());
415+ $ this ->_getSession ()->setCustomerFormData ($ this ->retrieveFormattedFormData ($ customer ));
416416 $ returnToEdit = true ;
417417 }
418418 }
@@ -553,21 +553,16 @@ private function disableAddressValidation($customer)
553553 /**
554554 * Retrieve formatted form data
555555 *
556+ * @param CustomerInterface $customer
556557 * @return array
557558 */
558- private function retrieveFormattedFormData (): array
559+ private function retrieveFormattedFormData (CustomerInterface $ customer ): array
559560 {
560561 $ originalRequestData = $ this ->getRequest ()->getPostValue ();
562+ $ customerData = $ this ->customerMapper ->toFlatArray ($ customer );
561563
562564 /* Customer data filtration */
563565 if (isset ($ originalRequestData ['customer ' ])) {
564- $ customerData = $ this ->_extractData (
565- 'adminhtml_customer ' ,
566- CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER ,
567- [],
568- 'customer '
569- );
570-
571566 $ customerData = array_intersect_key ($ customerData , $ originalRequestData ['customer ' ]);
572567 $ originalRequestData ['customer ' ] = array_merge ($ originalRequestData ['customer ' ], $ customerData );
573568 }
0 commit comments