Add a Date Picker Field to System.xml for Custom Magento Module
As an experienced Magento developer, I often encounter requests to add custom fields to the admin’s system configuration area using system.xml. One common requirement is adding a date field with its corresponding date picker. Let me guide you through this process step-by-step.
Understanding the Problem
In your case, you’re trying to extend the existing system.xml file for a custom module located at app/code/local/Namespace/Module/etc/system.xml. You want to add a new field that includes a date input field and automatically generates a date picker when accessed in the admin panel.
Analyzing the Given Code
You’ve tried to create a custom block called Datefield.php that extends Mage_Adminhtml_Block_System_Config_Form_Field. Inside the _getElementHtml() method, you’re calling Varien_Data_Form_Element_Abstract, but you’re unsure if it should be replaced with Varien_Data_Form_Element_Date.
The Correct Approach
To add a date field to your system.xml configuration, follow these steps:
- Create the Date Picker Method
First, you need to create a method inside your custom module’s config file (usually located at app/code/local/Namespace/Module/Model/System/Config.php).
class Namespace_Module_Model_System_Config extends Mage_Core_Model_Config_Backend_System {
public function getDateFieldHtml($field, $value){
return '<input type="text" name="'.$field.'" id="'.$this->getElementId($field).'" value="'.$value.'" class="input-text admin__field-control" data-ui-id="field-'.$field.'-control">';
}
}
- Update the system.xml File
Next, add your new field inside the <fields> section of your existing system.xml file.
<fields>
<my_date_field translate="label">
<label>Choose date</label>
<frontend_type>datefield</frontend_type>
<backend_model>Namespace_Module_Model_System_Config</backend_model>
<sort_order>10</sort_order>
</my_date_field>
</fields>
- Create the Date Field Block
Finally, you need to create a new block that will handle rendering the date field. In your case, this would be Namespace_Module_Block_Datefield.php.
class Namespace_Module_Block_Datefield extends Mage_Adminhtml_Block_System_Config_Form_Field {
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element) {
return '<input type="text" name="'.$this->getElement()->getName().'" id="'.$this->getElement()->getId().'" value="'.$this->htmlEscape($this->getElement()->getValue()).'" class="input-text admin__field-control" data-ui-id="field-'.$this->getElement()->getName().'-control">';
}
}
Conclusion
To add a date field with a built-in date picker to your custom Magento module’s system.xml configuration, you need to follow these key steps:
-
Create the
DateField.phpmethod insideapp/code/local/Namespace/Module/Model/System/Config.php. -
Update your existing
system.xmlfile to include the new field. -
Create a new block called
Namespace_Module_Block_Datefield.phpthat extendsMage_Adminhtml_Block_System_Config_Form_Field. -
Ensure you have the proper dependencies and namespaces set up in your module’s config file (
app/code/local/Namespace/Module/etc/config.xml).
Remember to clear your cache after making these changes.
For more detailed guides on Magento configuration, check out our series on system.xml customization at LaravelCompany.
Happy developing!