Magento: Add Custom Selection List in System Configuration
This tutorial shows how to add a custom select list in configuration settings of a custom Magento module.
Suppose, your module’s namespace is YourNamespace and your module’s name is YourModule. And, you want to add a selection list of Brands that contains mobile brand names like HTC, Samsung, Sony, Apple, etc.
Here is the code to add in your module’s system.xml file.
Note: I have set Group Name as ‘yourgroupname’, Field Name as ‘yourfield’. You can change that to your desired name.
<sections> ... ... <groups> <yourgroupname> <fields> <yourfield translate="label comment"> <label>Select Brands</label> <comment>Custom Selection List</comment> <frontend_type>select</frontend_type> <source_model>yourmodule/system_config_source_brands</source_model> <sort_order>10</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> </yourfield> </fields> </yourgroupname> </groups> </sections>
From the above system.xml code, you can see that the source model is defined as “yourmodule/system_config_source_brands“. So, you have to create a new model class in your module.
Class name will be YourNamespace_YourModule_Model_System_Config_Source_Brands and the file location will be YourNamespace/YourModule/Model/System/Config/Source/Brands.php.
Here is the class code:
<?php /** * Used in creating options config value selection * */ class YourNamespace_YourModule_Model_System_Config_Source_Brands { /** * Options getter * * @return array */ public function toOptionArray() { return array( array('value' => '', 'label'=>Mage::helper('yourmodule')->__('None')), array('value' => 1, 'label'=>Mage::helper('yourmodule')->__('Samsung')), array('value' => 2, 'label'=>Mage::helper('yourmodule')->__('Sony')), array('value' => 3, 'label'=>Mage::helper('yourmodule')->__('Apple')), ); } /** * Get options in "key-value" format * * @return array */ public function toArray() { return array( '' => Mage::helper('yourmodule')->__('None'), 1 => Mage::helper('yourmodule')->__('Samsung'), 2 => Mage::helper('yourmodule')->__('Sony'), 3 => Mage::helper('yourmodule')->__('Apple'), ); } }
That model class is defined in your module’s config.xml file.
<global> <models> <yourmodule> <class>YourNamespace_YourModule_Model</class> ... ... </yourmodule> ... ... </models> ... ... </global>
Hope this helps. Enjoy…. 🙂