/** * Output the region element and javasctipt that makes it dependent from country element * * @param \Magento\Framework\Data\Form\Element\AbstractElement $element * @return string * * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element) { if ($country = $element->getForm()->getElement('country_id')) { $countryId = $country->getValue(); } else { return $element->getDefaultHtml(); } $regionId = $element->getForm()->getElement('region_id')->getValue(); $html = '<div class="field field-state required">'; $element->setClass('input-text'); $element->setRequired(true); $html .= $element->getLabelHtml() . '<div class="control">'; $html .= $element->getElementHtml(); $selectName = str_replace('region', 'region_id', $element->getName()); $selectId = $element->getHtmlId() . '_id'; $html .= '<select id="' . $selectId . '" name="' . $selectName . '" class="select required-entry" style="display:none">'; $html .= '<option value="">' . __('Please select') . '</option>'; $html .= '</select>'; $html .= '<script>' . "\n"; $html .= 'require(["prototype", "mage/adminhtml/form"], function(){'; $html .= '$("' . $selectId . '").setAttribute("defaultValue", "' . $regionId . '");' . "\n"; $html .= 'new regionUpdater("' . $country->getHtmlId() . '", "' . $element->getHtmlId() . '", "' . $selectId . '", ' . $this->_directoryHelper->getRegionJson() . ');' . "\n"; $html .= '});'; $html .= '</script>' . "\n"; $html .= '</div></div>' . "\n"; return $html; }
public function testGetRegionJson() { $countries = array(new \Magento\Framework\Object(array('country_id' => 'Country1')), new \Magento\Framework\Object(array('country_id' => 'Country2'))); $countryIterator = new \ArrayIterator($countries); $this->_countryCollection->expects($this->atLeastOnce())->method('getIterator')->will($this->returnValue($countryIterator)); $regions = array(new \Magento\Framework\Object(array('country_id' => 'Country1', 'region_id' => 'r1', 'code' => 'r1-code', 'name' => 'r1-name')), new \Magento\Framework\Object(array('country_id' => 'Country1', 'region_id' => 'r2', 'code' => 'r2-code', 'name' => 'r2-name')), new \Magento\Framework\Object(array('country_id' => 'Country2', 'region_id' => 'r3', 'code' => 'r3-code', 'name' => 'r3-name'))); $regionIterator = new \ArrayIterator($regions); $this->_regionCollection->expects($this->once())->method('addCountryFilter')->with(array('Country1', 'Country2'))->will($this->returnSelf()); $this->_regionCollection->expects($this->once())->method('load'); $this->_regionCollection->expects($this->once())->method('getIterator')->will($this->returnValue($regionIterator)); $expectedDataToEncode = array('config' => array('show_all_regions' => false, 'regions_required' => array()), 'Country1' => array('r1' => array('code' => 'r1-code', 'name' => 'r1-name'), 'r2' => array('code' => 'r2-code', 'name' => 'r2-name')), 'Country2' => array('r3' => array('code' => 'r3-code', 'name' => 'r3-name'))); $this->_coreHelper->expects($this->once())->method('jsonEncode')->with(new \PHPUnit_Framework_Constraint_IsIdentical($expectedDataToEncode))->will($this->returnValue('encoded_json')); // Test $result = $this->_object->getRegionJson(); $this->assertEquals('encoded_json', $result); }
/** * @param AbstractElement $element * @return string */ protected function _getElementHtml(AbstractElement $element) { $html = parent::_getElementHtml($element); $js = '<script type="text/javascript"> var updater = new RegionUpdater("tax_defaults_country", "none", "tax_defaults_region", %s, "nullify"); if(updater.lastCountryId) { var tmpRegionId = $("tax_defaults_region").value; var tmpCountryId = updater.lastCountryId; updater.lastCountryId=false; updater.update(); updater.lastCountryId = tmpCountryId; $("tax_defaults_region").value = tmpRegionId; } else { updater.update(); } </script>'; $html .= sprintf($js, $this->_directoryHelper->getRegionJson()); return $html; }
/** * @param AbstractElement $element * @return string */ protected function _getElementHtml(AbstractElement $element) { $html = parent::_getElementHtml($element); $html .= "<script type=\"text/javascript\">" . "require(['mage/adminhtml/form'], function(){" . "window.updater = new RegionUpdater('tax_defaults_country'," . " 'tax_region', 'tax_defaults_region', " . $this->_directoryHelper->getRegionJson() . ", 'disable');});</script>"; return $html; }