collection = $addressCollectionFactory->create(); $this->collection->addAttributeToSelect('*'); $this->customerRepository = $customerRepository; $this->allowToShowHiddenAttributes = $allowToShowHiddenAttributes; $this->context = $context; $this->fileUploaderDataResolver = $fileUploaderDataResolver; $this->attributeMetadataResolver = $attributeMetadataResolver; $this->meta['general']['children'] = $this->getAttributesMeta( $eavConfig->getEntityType('customer_address') ); } /** * Get Addresses data and process customer default billing & shipping addresses * * @return array * @throws \Magento\Framework\Exception\LocalizedException * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function getData(): array { if (null !== $this->loadedData) { return $this->loadedData; } $items = $this->collection->getItems(); /** @var Address $item */ foreach ($items as $item) { $addressId = $item->getEntityId(); $item->load($addressId); $this->loadedData[$addressId] = $item->getData(); $customerId = $this->loadedData[$addressId]['parent_id']; /** @var \Magento\Customer\Model\Customer $customer */ $customer = $this->customerRepository->getById($customerId); $defaultBilling = $customer->getDefaultBilling(); $defaultShipping = $customer->getDefaultShipping(); $this->prepareAddressData($addressId, $this->loadedData, $defaultBilling, $defaultShipping); $this->fileUploaderDataResolver->overrideFileUploaderData($item, $this->loadedData[$addressId]); } if (null === $this->loadedData) { $this->loadedData[''] = $this->getDefaultData(); } return $this->loadedData; } /** * Prepare address data * * @param int $addressId * @param array $addresses * @param string|null $defaultBilling * @param string|null $defaultShipping * @return void */ private function prepareAddressData($addressId, array &$addresses, $defaultBilling, $defaultShipping): void { if (null !== $defaultBilling && $addressId === $defaultBilling) { $addresses[$addressId]['default_billing'] = '1'; } if (null !== $defaultShipping && $addressId === $defaultShipping) { $addresses[$addressId]['default_shipping'] = '1'; } foreach ($this->meta['general']['children'] as $attributeName => $attributeMeta) { if ($attributeMeta['arguments']['data']['config']['dataType'] === Multiline::NAME && isset($this->loadedData[$addressId][$attributeName]) && !\is_array($this->loadedData[$addressId][$attributeName]) ) { $this->loadedData[$addressId][$attributeName] = explode( "\n", $this->loadedData[$addressId][$attributeName] ); } } } /** * Get default customer data for adding new address * * @throws \Magento\Framework\Exception\LocalizedException * @throws \Magento\Framework\Exception\NoSuchEntityException * @return array */ private function getDefaultData(): array { $parentId = $this->context->getRequestParam('parent_id'); $customer = $this->customerRepository->getById($parentId); $data = [ 'parent_id' => $parentId, 'firstname' => $customer->getFirstname(), 'lastname' => $customer->getLastname() ]; return $data; } /** * 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) { if (\in_array($attribute->getFrontendInput(), $this->bannedInputTypes, true)) { continue; } if (\in_array($attribute->getAttributeCode(), self::$attributesToEliminate, true)) { continue; } $meta[$attribute->getAttributeCode()] = $this->attributeMetadataResolver->getAttributesMeta( $attribute, $entityType, $this->allowToShowHiddenAttributes, $this->getRequestFieldName() ); } $this->attributeMetadataResolver->processWebsiteMeta($meta); return $meta; } }