Пример #1
0
 /**
  * Retrieve name prefix drop-down options
  *
  * @return array|bool
  */
 public function getPrefixOptions()
 {
     $prefixOptions = $this->_customerHelper->getNamePrefixOptions();
     if ($this->getObject() && !empty($prefixOptions)) {
         $oldPrefix = $this->escapeHtml(trim($this->getObject()->getPrefix()));
         $prefixOptions[$oldPrefix] = $oldPrefix;
     }
     return $prefixOptions;
 }
Пример #2
0
 /**
  * Prepare Form and add elements to form
  *
  * @return $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function _prepareForm()
 {
     $fieldset = $this->_form->addFieldset('main', array('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->_customerHelper->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->_customerHelper->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 (is_null($this->_form->getElement('country_id')->getValue())) {
         $this->_form->getElement('country_id')->setValue($this->_coreData->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;
 }
Пример #3
0
 /**
  * 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->_customerHelper->getNamePrefixOptions($this->_getCustomerDataObject()->getStoreId());
             $prevSibling = $fieldset->getForm()->getElement('group_id')->getId();
         }
         if ($elementName == 'suffix') {
             $options = $this->_customerHelper->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);
         }
     }
 }
Пример #4
0
 /**
  * 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', array('legend' => __("Edit Customer's Address")));
     $account = $customerData['account'];
     $this->_addressBuilder->populateWithArray(array());
     if (!empty($account) && isset($account['store_id'])) {
         $this->_addressBuilder->setCountryId($this->_coreData->getDefaultCountry($this->_storeManager->getStore($account['store_id'])));
     } else {
         $this->_addressBuilder->setCountryId($this->_coreData->getDefaultCountry());
     }
     $address = $this->_addressBuilder->create();
     $addressForm = $this->_metadataFormFactory->create('customer_address', 'adminhtml_customer_address', AddressConverter::toFlatArray($address));
     $attributes = $addressForm->getAttributes();
     if (isset($attributes['street'])) {
         if ($attributes['street']->getMultilineCount() <= 0) {
             $attributes['street'] = $this->_attributeMetadataBuilder->populate($attributes['street'])->setMultilineCount(self::DEFAULT_STREET_LINES_COUNT)->create();
         }
     }
     foreach ($attributes as $key => $attribute) {
         $attributes[$key] = $this->_attributeMetadataBuilder->populate($attribute)->setFrontendLabel(__($attribute->getFrontendLabel()))->setVisible(false)->create();
     }
     $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');
     }
     if ($this->isReadonly()) {
         foreach ($this->_metadataService->getAllAddressAttributeMetadata() 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->_customerHelper->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->_customerHelper->getNameSuffixOptions($customerStoreId);
         if (!empty($suffixOptions)) {
             $fieldset->removeField($suffixElement->getId());
             $suffixField = $fieldset->addField($suffixElement->getId(), 'select', $suffixElement->getData(), $form->getElement('lastname')->getId());
             $suffixField->setValues($suffixOptions);
         }
     }
     $this->assign('customer', $this->_customerBuilder->populateWithArray($account)->create());
     $addressCollection = array();
     foreach ($customerData['address'] as $key => $addressData) {
         $addressCollection[$key] = $this->_addressBuilder->populateWithArray($addressData)->create();
     }
     $this->assign('addressCollection', $addressCollection);
     $form->setValues(AddressConverter::toFlatArray($address));
     $this->setForm($form);
     return $this;
 }