/** * Retrieve customer attribute instance * * @param string $attributeCode * @return \Magento\Customer\Api\Data\AttributeMetadataInterface|null */ protected function _getAttribute($attributeCode) { try { return $this->customerMetadata->getAttributeMetadata($attributeCode); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { return null; } }
/** * Edit/View Existing Customer form fields * * @param \Magento\Framework\Data\Form\Element\Fieldset $fieldset * @return string[] Values to set on the form */ protected function _addEditCustomerFormFields($fieldset) { $fieldset->getForm()->getElement('created_in')->setDisabled('disabled'); $fieldset->getForm()->getElement('website_id')->setDisabled('disabled'); $customerData = $this->_getCustomerDataObject(); if ($customerData->getId() && $this->_accountManagement->isReadonly($customerData->getId())) { return []; } // Prepare customer confirmation control (only for existing customers) $confirmationStatus = $this->_accountManagement->getConfirmationStatus($customerData->getId()); $confirmationKey = $customerData->getConfirmation(); if ($confirmationStatus != AccountManagementInterface::ACCOUNT_CONFIRMED) { $confirmationAttr = $this->_customerMetadata->getAttributeMetadata('confirmation'); if (!$confirmationKey) { $confirmationKey = $this->_getRandomConfirmationKey(); } $element = $fieldset->addField('confirmation', 'select', ['name' => 'confirmation', 'label' => __($confirmationAttr->getFrontendLabel())]); $element->setEntityAttribute($confirmationAttr); $element->setValues(['' => 'Confirmed', $confirmationKey => 'Not confirmed']); // Prepare send welcome email checkbox if customer is not confirmed // no need to add it, if website ID is empty if ($customerData->getConfirmation() && $customerData->getWebsiteId()) { $fieldset->addField('sendemail', 'checkbox', ['name' => 'sendemail', 'label' => __('Send Welcome Email after Confirmation')]); return ['sendemail' => '1']; } } return []; }
/** * Get attribute metadata. * * @param string $attributeCode * @return \Magento\Customer\Api\Data\AttributeMetadataInterface|null */ private function getAttributeMetadata($attributeCode) { try { return $this->customerMetadata->getAttributeMetadata($attributeCode); } catch (NoSuchEntityException $e) { return null; } }
public function testGetCustomerAttributeMetadataNoSuchEntity() { try { $this->_service->getAttributeMetadata('20'); $this->fail('Expected exception not thrown.'); } catch (NoSuchEntityException $e) { $this->assertEquals('No such entity with entityType = customer, attributeCode = 20', $e->getMessage()); } }
/** * Concatenate all customer name parts into full customer name. * * @param CustomerInterface $customerData * @return string */ public function getCustomerName(CustomerInterface $customerData) { $name = ''; $prefixMetadata = $this->_customerMetadataService->getAttributeMetadata('prefix'); if ($prefixMetadata->isVisible() && $customerData->getPrefix()) { $name .= $customerData->getPrefix() . ' '; } $name .= $customerData->getFirstname(); $middleNameMetadata = $this->_customerMetadataService->getAttributeMetadata('middlename'); if ($middleNameMetadata->isVisible() && $customerData->getMiddlename()) { $name .= ' ' . $customerData->getMiddlename(); } $name .= ' ' . $customerData->getLastname(); $suffixMetadata = $this->_customerMetadataService->getAttributeMetadata('suffix'); if ($suffixMetadata->isVisible() && $customerData->getSuffix()) { $name .= ' ' . $customerData->getSuffix(); } return $name; }
/** * Update customer gender value * Method set gender label instead of id value * @return void */ private function setGenderValue() { $value = $this->getData(self::$genderAttributeCode); if (!$value) { $this->setCustomAttribute(self::$genderAttributeCode, 'N/A'); return; } try { $attributeMetadata = $this->customerMetadata->getAttributeMetadata(self::$genderAttributeCode); $option = $attributeMetadata->getOptions()[$value]; $this->setCustomAttribute(self::$genderAttributeCode, $option->getLabel()); } catch (NoSuchEntityException $e) { $this->setCustomAttribute(self::$genderAttributeCode, 'N/A'); } }
/** * Get string with frontend validation classes for attribute * * @param string $attributeCode * @return string * * @SuppressWarnings(PHPMD.NPathComplexity) */ public function getAttributeValidationClass($attributeCode) { $class = ''; try { /** @var $attribute AttributeMetadataInterface */ $attribute = isset($this->_attributes[$attributeCode]) ? $this->_attributes[$attributeCode] : $this->_addressMetadataService->getAttributeMetadata($attributeCode); $class = $attribute ? $attribute->getFrontendClass() : ''; if (in_array($attributeCode, ['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 AttributeMetadataInterface */ $customerAttribute = $this->_customerMetadataService->getAttributeMetadata($attributeCode); $class .= $customerAttribute && $customerAttribute->isVisible() ? $customerAttribute->getFrontendClass() : ''; $class = implode(' ', array_unique(array_filter(explode(' ', $class)))); } } catch (NoSuchEntityException $e) { // the attribute does not exist so just return an empty string } return $class; }