/** * Retrieve name suffix drop-down options * * @return array|bool */ public function getSuffixOptions() { $suffixOptions = $this->options->getNameSuffixOptions(); if ($this->getObject() && !empty($suffixOptions)) { $oldSuffix = $this->escapeHtml(trim($this->getObject()->getSuffix())); $suffixOptions[$oldSuffix] = $oldSuffix; } return $suffixOptions; }
/** * Prepare Form and add elements to form * * @return $this * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function _prepareForm() { $fieldset = $this->_form->addFieldset('main', ['no_container' => true]); $addressForm = $this->_customerFormFactory->create('customer_address', 'adminhtml_customer_address'); $attributes = $addressForm->getAttributes(); $this->_addAttributesToForm($attributes, $fieldset); $prefixElement = $this->_form->getElement('prefix'); if ($prefixElement) { $prefixOptions = $this->options->getNamePrefixOptions($this->getStore()); if (!empty($prefixOptions)) { $fieldset->removeField($prefixElement->getId()); $prefixField = $fieldset->addField($prefixElement->getId(), 'select', $prefixElement->getData(), '^'); $prefixField->setValues($prefixOptions); if ($this->getAddressId()) { $prefixField->addElementValues($this->getAddress()->getPrefix()); } } } $suffixElement = $this->_form->getElement('suffix'); if ($suffixElement) { $suffixOptions = $this->options->getNameSuffixOptions($this->getStore()); if (!empty($suffixOptions)) { $fieldset->removeField($suffixElement->getId()); $suffixField = $fieldset->addField($suffixElement->getId(), 'select', $suffixElement->getData(), $this->_form->getElement('lastname')->getId()); $suffixField->setValues($suffixOptions); if ($this->getAddressId()) { $suffixField->addElementValues($this->getAddress()->getSuffix()); } } } $regionElement = $this->_form->getElement('region_id'); if ($regionElement) { $regionElement->setNoDisplay(true); } $this->_form->setValues($this->getFormValues()); if ($this->_form->getElement('country_id')->getValue()) { $countryId = $this->_form->getElement('country_id')->getValue(); $this->_form->getElement('country_id')->setValue(null); foreach ($this->_form->getElement('country_id')->getValues() as $country) { if ($country['value'] == $countryId) { $this->_form->getElement('country_id')->setValue($countryId); } } } if ($this->_form->getElement('country_id')->getValue() === null) { $this->_form->getElement('country_id')->setValue($this->directoryHelper->getDefaultCountry($this->getStore())); } // Set custom renderer for VAT field if needed $vatIdElement = $this->_form->getElement('vat_id'); if ($vatIdElement && $this->getDisplayVatValidationButton() !== false) { $vatIdElement->setRenderer($this->getLayout()->createBlock('Magento\\Customer\\Block\\Adminhtml\\Sales\\Order\\Address\\Form\\Renderer\\Vat')->setJsVariablePrefix($this->getJsVariablePrefix())); } return $this; }
/** * @param $customerData * @param $isSingleStoreMode * @param $canModifyCustomer * * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ private function _setupStoreMode($customerData, $isSingleStoreMode, $canModifyCustomer) { $backendSessionMock = $this->getMock('\\Magento\\Backend\\Model\\Session', ['getCustomerData'], [], '', false); $backendSessionMock->expects($this->any())->method('getCustomerData')->will($this->returnValue([])); $layoutMock = $this->getMock('\\Magento\\Framework\\View\\Layout\\Element\\Layout', ['createBlock'], [], '', false); $layoutMock->expects($this->at(0))->method('createBlock')->with('Magento\\Customer\\Block\\Adminhtml\\Edit\\Renderer\\Attribute\\Group')->will($this->returnValue($this->objectManagerHelper->getObject('Magento\\Customer\\Block\\Adminhtml\\Edit\\Renderer\\Attribute\\Group'))); $layoutMock->expects($this->at(1))->method('createBlock')->with('Magento\\Backend\\Block\\Store\\Switcher\\Form\\Renderer\\Fieldset\\Element')->will($this->returnValue($this->objectManagerHelper->getObject('Magento\\Backend\\Block\\Store\\Switcher\\Form\\Renderer\\Fieldset\\Element'))); if (empty($customerData['id'])) { $layoutMock->expects($this->at(2))->method('createBlock')->with('Magento\\Customer\\Block\\Adminhtml\\Edit\\Renderer\\Attribute\\Sendemail')->will($this->returnValue($this->objectManagerHelper->getObject('Magento\\Customer\\Block\\Adminhtml\\Edit\\Renderer\\Attribute\\Sendemail'))); } $urlBuilderMock = $this->getMock('\\Magento\\Backend\\Model\\Url', ['getBaseUrl'], [], '', false); $urlBuilderMock->expects($this->once())->method('getBaseUrl')->will($this->returnValue('someUrl')); $storeManagerMock = $this->getMock('Magento\\Store\\Model\\StoreManager', [], [], '', false); $storeManagerMock->expects($this->any())->method('isSingleStoreMode')->will($this->returnValue($isSingleStoreMode)); $customerObject = $this->getMock('\\Magento\\Customer\\Api\\Data\\CustomerInterface'); if (!empty($customerData['id'])) { $customerObject->expects($this->any())->method('getId')->will($this->returnValue($customerData['id'])); } $fieldset = $this->getMockBuilder('\\Magento\\Framework\\Data\\Form\\Element\\Fieldset')->setMethods(['getForm', 'addField', 'removeField'])->disableOriginalConstructor()->getMock(); $accountForm = $this->getMockBuilder('Magento\\Framework\\Data\\Form')->setMethods(['create', 'addFieldset', 'getElement', 'setValues'])->disableOriginalConstructor()->getMock(); $accountForm->expects($this->any())->method('addFieldset')->with('base_fieldset')->will($this->returnValue($fieldset)); $accountForm->expects($this->any())->method('setValues')->will($this->returnValue($accountForm)); $fieldset->expects($this->any())->method('getForm')->will($this->returnValue($accountForm)); $formElement = $this->getMockBuilder('\\Magento\\Framework\\Data\\Form\\Element\\Select')->setMethods(['setRenderer', 'addClass', 'setDisabled'])->disableOriginalConstructor()->getMock(); $formElement->expects($this->any())->method('setRenderer')->will($this->returnValue(null)); $formElement->expects($this->any())->method('addClass')->will($this->returnValue(null)); $formElement->expects($this->any())->method('setDisabled')->will($this->returnValue(null)); $accountForm->expects($this->any())->method('getElement')->withAnyParameters()->will($this->returnValue($formElement)); $fieldset->expects($this->any())->method('addField')->will($this->returnValue($formElement)); $customerForm = $this->getMock('\\Magento\\Customer\\Model\\Metadata\\Form', ['getAttributes'], [], '', false); $customerForm->expects($this->any())->method('getAttributes')->will($this->returnValue([])); $this->contextMock->expects($this->any())->method('getBackendSession')->will($this->returnValue($backendSessionMock)); $this->contextMock->expects($this->any())->method('getLayout')->will($this->returnValue($layoutMock)); $this->contextMock->expects($this->any())->method('getUrlBuilder')->will($this->returnValue($urlBuilderMock)); $this->contextMock->expects($this->any())->method('getStoreManager')->will($this->returnValue($storeManagerMock)); $this->customerFactoryMock->expects($this->any())->method('create')->will($this->returnValue($customerObject)); $this->dataObjectHelperMock->expects($this->once())->method('populateWithArray'); $this->options->expects($this->any())->method('getNamePrefixOptions')->will($this->returnValue(['Pref1', 'Pref2'])); $this->options->expects($this->any())->method('getNameSuffixOptions')->will($this->returnValue(['Suf1', 'Suf2'])); $this->formFactoryMock->expects($this->any())->method('create')->will($this->returnValue($accountForm)); $this->extensibleDataObjectConverterMock->expects($this->any())->method('toFlatArray')->will($this->returnValue($customerData)); $this->customerFormFactoryMock->expects($this->any())->method('create')->with('customer', 'adminhtml_customer', $this->extensibleDataObjectConverterMock->toFlatArray($customerObject, [], '\\Magento\\Customer\\Api\\Data\\CustomerInterface'))->will($this->returnValue($customerForm)); $this->accountManagementMock->expects($this->any())->method('isReadOnly')->withAnyParameters()->will($this->returnValue(!$canModifyCustomer)); $this->accountManagementMock->expects($this->any())->method('getConfirmationStatus')->withAnyParameters()->will($this->returnValue(AccountManagementInterface::ACCOUNT_CONFIRMED)); }
/** * Check if type of Prefix and Suffix elements should be changed from text to select and change it if need. * * @param string $elementName * @param \Magento\Framework\Data\Form\Element\Fieldset $fieldset * @return null */ protected function _checkElementType($elementName, $fieldset) { $possibleElements = ['prefix', 'suffix']; if (!in_array($elementName, $possibleElements)) { return; } $element = $fieldset->getForm()->getElement($elementName); if ($element) { if ($elementName == 'prefix') { $options = $this->options->getNamePrefixOptions($this->_getCustomerDataObject()->getStoreId()); $prevSibling = $fieldset->getForm()->getElement('group_id')->getId(); } if ($elementName == 'suffix') { $options = $this->options->getNameSuffixOptions($this->_getCustomerDataObject()->getStoreId()); $prevSibling = $fieldset->getForm()->getElement('lastname')->getId(); } if (!empty($options)) { $fieldset->removeField($element->getId()); $elementField = $fieldset->addField($element->getId(), 'select', $element->getData(), $prevSibling); $elementField->setValues($options); } } }
/** * Initialize form object * * @return $this * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function initForm() { $customerData = $this->_backendSession->getCustomerData(); /** @var \Magento\Framework\Data\Form $form */ $form = $this->_formFactory->create(); $fieldset = $form->addFieldset('address_fieldset', ['legend' => __("Edit Customer's Address")]); $account = $customerData['account']; $address = $this->addressDataFactory->create(); if (!empty($account) && isset($account['store_id'])) { $address->setCountryId($this->_directoryHelper->getDefaultCountry($this->_storeManager->getStore($account['store_id']))); } else { $address->setCountryId($this->_directoryHelper->getDefaultCountry()); } $addressForm = $this->_metadataFormFactory->create('customer_address', 'adminhtml_customer_address', $this->addressMapper->toFlatArray($address)); $attributes = $addressForm->getAttributes(); if (isset($attributes['street'])) { if ($attributes['street']->getMultilineCount() <= 0) { $attributes['street']->setMultilineCount(self::DEFAULT_STREET_LINES_COUNT); } } foreach ($attributes as $key => $attribute) { $attributes[$key]->setFrontendLabel(__($attribute->getFrontendLabel()))->setIsVisible(false); } $this->_setFieldset($attributes, $fieldset); $regionElement = $form->getElement('region'); if ($regionElement) { $regionElement->setRenderer($this->_regionFactory->create()); } $regionElement = $form->getElement('region_id'); if ($regionElement) { $regionElement->setNoDisplay(true); } $country = $form->getElement('country_id'); if ($country) { $country->addClass('countries'); } $postcode = $form->getElement('postcode'); if ($postcode) { $postcode->removeClass('required-entry')->setRequired(!$this->_directoryHelper->isZipCodeOptional($address->getCountryId())); } if ($this->isReadonly()) { foreach ($this->_addressMetadataService->getAllAttributesMetadata() as $attribute) { $element = $form->getElement($attribute->getAttributeCode()); if ($element) { $element->setReadonly(true, true); } } } $customerStoreId = null; if (!empty($account) && isset($account['id']) && isset($account['website_id'])) { $customerStoreId = $this->_storeManager->getWebsite($account['website_id'])->getDefaultStore()->getId(); } $prefixElement = $form->getElement('prefix'); if ($prefixElement) { $prefixOptions = $this->options->getNamePrefixOptions($customerStoreId); if (!empty($prefixOptions)) { $fieldset->removeField($prefixElement->getId()); $prefixField = $fieldset->addField($prefixElement->getId(), 'select', $prefixElement->getData(), '^'); $prefixField->setValues($prefixOptions); } } $suffixElement = $form->getElement('suffix'); if ($suffixElement) { $suffixOptions = $this->options->getNameSuffixOptions($customerStoreId); if (!empty($suffixOptions)) { $fieldset->removeField($suffixElement->getId()); $suffixField = $fieldset->addField($suffixElement->getId(), 'select', $suffixElement->getData(), $form->getElement('lastname')->getId()); $suffixField->setValues($suffixOptions); } } $customerDataObject = $this->customerDataFactory->create(); $this->dataObjectHelper->populateWithArray($customerDataObject, $account, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $this->assign('customer', $customerDataObject); $addressCollection = []; foreach ($customerData['address'] as $key => $addressData) { $addressDataObject = $this->addressDataFactory->create(); $this->dataObjectHelper->populateWithArray($addressDataObject, $addressData, '\\Magento\\Customer\\Api\\Data\\AddressInterface'); $addressCollection[$key] = $addressDataObject; } $this->assign('addressCollection', $addressCollection); $form->setValues($this->addressMapper->toFlatArray($address)); $this->setForm($form); return $this; }