DataProviderWithDefaultAddresses.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Customer\Model\Customer;
  8. use Magento\Customer\Model\Address;
  9. use Magento\Customer\Model\Customer;
  10. use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
  11. use Magento\Eav\Model\Config;
  12. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  13. use Magento\Eav\Model\Entity\Type;
  14. use Magento\Framework\Session\SessionManagerInterface;
  15. use Magento\Customer\Model\FileUploaderDataResolver;
  16. use Magento\Customer\Model\AttributeMetadataResolver;
  17. /**
  18. * Refactored version of Magento\Customer\Model\Customer\DataProvider with eliminated usage of addresses collection.
  19. */
  20. class DataProviderWithDefaultAddresses extends \Magento\Ui\DataProvider\AbstractDataProvider
  21. {
  22. /**
  23. * @var array
  24. */
  25. private $loadedData = [];
  26. /**
  27. * @var SessionManagerInterface
  28. */
  29. private $session;
  30. /**
  31. * Customer fields that must be removed
  32. *
  33. * @var array
  34. */
  35. private static $forbiddenCustomerFields = [
  36. 'password_hash',
  37. 'rp_token',
  38. 'confirmation',
  39. ];
  40. /**
  41. * Allow to manage attributes, even they are hidden on storefront
  42. *
  43. * @var bool
  44. */
  45. private $allowToShowHiddenAttributes;
  46. /**
  47. * @var \Magento\Directory\Model\CountryFactory
  48. */
  49. private $countryFactory;
  50. /**
  51. * @var FileUploaderDataResolver
  52. */
  53. private $fileUploaderDataResolver;
  54. /**
  55. * @var AttributeMetadataResolver
  56. */
  57. private $attributeMetadataResolver;
  58. /**
  59. * @param string $name
  60. * @param string $primaryFieldName
  61. * @param string $requestFieldName
  62. * @param CustomerCollectionFactory $customerCollectionFactory
  63. * @param Config $eavConfig
  64. * @param \Magento\Directory\Model\CountryFactory $countryFactory
  65. * @param SessionManagerInterface $session
  66. * @param FileUploaderDataResolver $fileUploaderDataResolver
  67. * @param AttributeMetadataResolver $attributeMetadataResolver
  68. * @param bool $allowToShowHiddenAttributes
  69. * @param array $meta
  70. * @param array $data
  71. * @throws \Magento\Framework\Exception\LocalizedException
  72. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  73. */
  74. public function __construct(
  75. string $name,
  76. string $primaryFieldName,
  77. string $requestFieldName,
  78. CustomerCollectionFactory $customerCollectionFactory,
  79. Config $eavConfig,
  80. \Magento\Directory\Model\CountryFactory $countryFactory,
  81. SessionManagerInterface $session,
  82. FileUploaderDataResolver $fileUploaderDataResolver,
  83. AttributeMetadataResolver $attributeMetadataResolver,
  84. $allowToShowHiddenAttributes = true,
  85. array $meta = [],
  86. array $data = []
  87. ) {
  88. parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
  89. $this->collection = $customerCollectionFactory->create();
  90. $this->collection->addAttributeToSelect('*');
  91. $this->allowToShowHiddenAttributes = $allowToShowHiddenAttributes;
  92. $this->session = $session;
  93. $this->countryFactory = $countryFactory;
  94. $this->fileUploaderDataResolver = $fileUploaderDataResolver;
  95. $this->attributeMetadataResolver = $attributeMetadataResolver;
  96. $this->meta['customer']['children'] = $this->getAttributesMeta(
  97. $eavConfig->getEntityType('customer')
  98. );
  99. }
  100. /**
  101. * Get data
  102. *
  103. * @return array
  104. */
  105. public function getData(): array
  106. {
  107. if (!empty($this->loadedData)) {
  108. return $this->loadedData;
  109. }
  110. $items = $this->collection->getItems();
  111. /** @var Customer $customer */
  112. foreach ($items as $customer) {
  113. $result['customer'] = $customer->getData();
  114. $this->fileUploaderDataResolver->overrideFileUploaderData($customer, $result['customer']);
  115. $result['customer'] = array_diff_key(
  116. $result['customer'],
  117. array_flip(self::$forbiddenCustomerFields)
  118. );
  119. unset($result['address']);
  120. $result['default_billing_address'] = $this->prepareDefaultAddress(
  121. $customer->getDefaultBillingAddress()
  122. );
  123. $result['default_shipping_address'] = $this->prepareDefaultAddress(
  124. $customer->getDefaultShippingAddress()
  125. );
  126. $result['customer_id'] = $customer->getId();
  127. $this->loadedData[$customer->getId()] = $result;
  128. }
  129. $data = $this->session->getCustomerFormData();
  130. if (!empty($data)) {
  131. $customerId = $data['customer']['entity_id'] ?? null;
  132. $this->loadedData[$customerId] = $data;
  133. $this->session->unsCustomerFormData();
  134. }
  135. return $this->loadedData;
  136. }
  137. /**
  138. * Prepare default address data.
  139. *
  140. * @param Address|false $address
  141. * @return array
  142. */
  143. private function prepareDefaultAddress($address): array
  144. {
  145. $addressData = [];
  146. if (!empty($address)) {
  147. $addressData = $address->getData();
  148. if (isset($addressData['street']) && !\is_array($address['street'])) {
  149. $addressData['street'] = explode("\n", $addressData['street']);
  150. }
  151. $addressData['country'] = $this->countryFactory->create()
  152. ->loadByCode($addressData['country_id'])->getName();
  153. }
  154. return $addressData;
  155. }
  156. /**
  157. * Get attributes meta
  158. *
  159. * @param Type $entityType
  160. * @return array
  161. * @throws \Magento\Framework\Exception\LocalizedException
  162. */
  163. private function getAttributesMeta(Type $entityType): array
  164. {
  165. $meta = [];
  166. $attributes = $entityType->getAttributeCollection();
  167. /* @var AbstractAttribute $attribute */
  168. foreach ($attributes as $attribute) {
  169. $meta[$attribute->getAttributeCode()] = $this->attributeMetadataResolver->getAttributesMeta(
  170. $attribute,
  171. $entityType,
  172. $this->allowToShowHiddenAttributes,
  173. $this->getRequestFieldName()
  174. );
  175. }
  176. $this->attributeMetadataResolver->processWebsiteMeta($meta);
  177. return $meta;
  178. }
  179. }