collection = $customerCollectionFactory->create(); $this->collection->addAttributeToSelect('*'); $this->allowToShowHiddenAttributes = $allowToShowHiddenAttributes; $this->session = $session; $this->countryFactory = $countryFactory; $this->fileUploaderDataResolver = $fileUploaderDataResolver; $this->attributeMetadataResolver = $attributeMetadataResolver; $this->meta['customer']['children'] = $this->getAttributesMeta( $eavConfig->getEntityType('customer') ); } /** * Get data * * @return array */ public function getData(): array { if (!empty($this->loadedData)) { return $this->loadedData; } $items = $this->collection->getItems(); /** @var Customer $customer */ foreach ($items as $customer) { $result['customer'] = $customer->getData(); $this->fileUploaderDataResolver->overrideFileUploaderData($customer, $result['customer']); $result['customer'] = array_diff_key( $result['customer'], array_flip(self::$forbiddenCustomerFields) ); unset($result['address']); $result['default_billing_address'] = $this->prepareDefaultAddress( $customer->getDefaultBillingAddress() ); $result['default_shipping_address'] = $this->prepareDefaultAddress( $customer->getDefaultShippingAddress() ); $result['customer_id'] = $customer->getId(); $this->loadedData[$customer->getId()] = $result; } $data = $this->session->getCustomerFormData(); if (!empty($data)) { $customerId = $data['customer']['entity_id'] ?? null; $this->loadedData[$customerId] = $data; $this->session->unsCustomerFormData(); } return $this->loadedData; } /** * Prepare default address data. * * @param Address|false $address * @return array */ private function prepareDefaultAddress($address): array { $addressData = []; if (!empty($address)) { $addressData = $address->getData(); if (isset($addressData['street']) && !\is_array($address['street'])) { $addressData['street'] = explode("\n", $addressData['street']); } $addressData['country'] = $this->countryFactory->create() ->loadByCode($addressData['country_id'])->getName(); } return $addressData; } /** * Get attributes meta * * @param Type $entityType * @return array * @throws \Magento\Framework\Exception\LocalizedException */ private function getAttributesMeta(Type $entityType): array { $meta = []; $attributes = $entityType->getAttributeCollection(); /* @var AbstractAttribute $attribute */ foreach ($attributes as $attribute) { $meta[$attribute->getAttributeCode()] = $this->attributeMetadataResolver->getAttributesMeta( $attribute, $entityType, $this->allowToShowHiddenAttributes, $this->getRequestFieldName() ); } $this->attributeMetadataResolver->processWebsiteMeta($meta); return $meta; } }