Question

How can I create a date field by using system.xml?

Was it helpful?

Solution

If you add the following in your system.xml

<frontend_type>text</frontend_type>
<frontend_model>namespace_module/adminhtml_system_config_date</frontend_model>

Then create the following file:

app/code/[codePool]/Namespace/Module/Block/Adminhtml/System/Config/Date.php

class Namespace_Module_Block_Adminhtml_System_Config_Date extends Mage_Adminhtml_Block_System_Config_Form_Field
{
    protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
    {
        $date = new Varien_Data_Form_Element_Date();
        $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);

        $data = array(
            'name'      => $element->getName(),
            'html_id'   => $element->getId(),
            'image'     => $this->getSkinUrl('images/grid-cal.gif'),
        );
        $date->setData($data);
        $date->setValue($element->getValue(), $format);
        $date->setFormat(Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT));
        $date->setForm($element->getForm());

        return $date->getElementHtml();
    }
}

This is assuming that you already have the blocks setup for this module in the config.xml

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top