*/ class Mage_Adminhtml_Model_Customer_Renderer_Region implements Varien_Data_Form_Element_Renderer_Interface { /** * Country region collections * * array( * [$countryId] => Varien_Data_Collection_Db * ) * * @var array */ static protected $_regionCollections; public function render(Varien_Data_Form_Element_Abstract $element) { $html = ''."\n"; $countryId = false; if ($country = $element->getForm()->getElement('country_id')) { $countryId = $country->getValue(); } $regionCollection = false; if ($countryId) { if (!isset(self::$_regionCollections[$countryId])) { self::$_regionCollections[$countryId] = Mage::getModel('directory/country') ->setId($countryId) ->getLoadedRegionCollection() ->toOptionArray(); } $regionCollection = self::$_regionCollections[$countryId]; } $regionId = intval($element->getForm()->getElement('region_id')->getValue()); $htmlAttributes = $element->getHtmlAttributes(); foreach ($htmlAttributes as $key => $attribute) { if ('type' === $attribute) { unset($htmlAttributes[$key]); break; } } // Output two elements - for 'region' and for 'region_id'. // Two elements are needed later upon form post - to properly set data to address model, // otherwise old value can be left in region_id attribute and saved to DB. // Depending on country selected either 'region' (input text) or 'region_id' (selectbox) is visible to user $regionHtmlName = $element->getName(); $regionIdHtmlName = str_replace('region', 'region_id', $regionHtmlName); $regionHtmlId = $element->getHtmlId(); $regionIdHtmlId = str_replace('region', 'region_id', $regionHtmlId); if ($regionCollection && count($regionCollection) > 0) { $elementClass = $element->getClass(); $html.= ''.$element->getLabelHtml().''; $html.= ''; $html .= '' . "\n"; $html .= ''; $html.= ''; $element->setClass($elementClass); } else { $element->setClass('input-text'); $html.= ''; $element->setRequired(false); $html.= ''; $html .= 'serialize($htmlAttributes) . "/>" . "\n"; $html .= ''; $html .= ''."\n"; } $html.= ''."\n"; return $html; } }