public function testGetAddressAttributeMetadataNoSuchEntity() { try { $this->_service->getAttributeMetadata('1'); $this->fail('Expected exception not thrown.'); } catch (NoSuchEntityException $e) { $this->assertEquals('No such entity with entityType = customer_address, attributeCode = 1', $e->getMessage()); } }
/** * Get string with frontend validation classes for attribute * * @param string $attributeCode * @return string * * @SuppressWarnings(PHPMD.NPathComplexity) */ public function getAttributeValidationClass($attributeCode) { /** @var $attribute \Magento\Customer\Service\V1\Data\Eav\AttributeMetadata */ $attribute = isset($this->_attributes[$attributeCode]) ? $this->_attributes[$attributeCode] : $this->_addressMetadataService->getAttributeMetadata($attributeCode); $class = $attribute ? $attribute->getFrontendClass() : ''; if (in_array($attributeCode, array('firstname', 'middlename', 'lastname', 'prefix', 'suffix', 'taxvat'))) { if ($class && !$attribute->isVisible()) { // address attribute is not visible thus its validation rules are not applied $class = ''; } /** @var $customerAttribute \Magento\Customer\Service\V1\Data\Eav\AttributeMetadata */ $customerAttribute = $this->_customerMetadataService->getAttributeMetadata($attributeCode); $class .= $customerAttribute && $customerAttribute->isVisible() ? $customerAttribute->getFrontendClass() : ''; $class = implode(' ', array_unique(array_filter(explode(' ', $class)))); } return $class; }
/** * Retrieve customer or customer address attribute instance * * @param string $attributeCode * @return \Magento\Customer\Service\V1\Data\Eav\AttributeMetadata|null * * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function _getAttribute($attributeCode) { if ($this->getForceUseCustomerAttributes() || $this->getObject() instanceof Customer) { return parent::_getAttribute($attributeCode); } try { $attribute = $this->_addressMetadataService->getAttributeMetadata($attributeCode); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { return null; } if ($this->getForceUseCustomerRequiredAttributes() && $attribute && !$attribute->isRequired()) { $customerAttribute = parent::_getAttribute($attributeCode); if ($customerAttribute && $customerAttribute->isRequired()) { $attribute = $customerAttribute; } } return $attribute; }