Magento Module Development Series – Part9 – Module – system.xml – Advanced

In this blog we will look at some advanced stuff that we can do through system.xml.

Specifically we will see how to

  • Create drop down in system.xml and show custom values in it, rather than using default magento classes only
  • How to create an import/export csv feature using system.xml


Before reading this blog make sure you understand basics of system.xml, if not go though this blog.

Custom Dropdown in system.xml

Below is the system.xml for a drop down

<config>
  <sections>
    <test translate="label">
      	<label>A Test Section</label>
      	<tab>general</tab>
      	<frontend_type>text</frontend_type>
     	 <sort_order>1000</sort_order>
      	<show_in_default>1</show_in_default>
      	<show_in_website>1</show_in_website>
      	<show_in_store>1</show_in_store>
			<groups>
       			 <!-- New groups go here -->
        			<sample translate="label">
          			<label>A Sample Group</label>
          			<frontend_type>text</frontend_type>
          			<sort_order>100</sort_order>
          			<show_in_default>1</show_in_default>
          			<show_in_website>1</show_in_website>
          			<show_in_store>1</show_in_store>
        							<fields>
           			 	<!-- New fields go here -->
            				<ENABLED translate="label comment">
              				<label>Enabled</label>
              				<comment><![CDATA[This text appears just beneath the field with a small arrow.<span class="notice">It can contain HTML formatting too!</span>]]></comment>
              				<frontend_type>select</frontend_type>
              				<source_model>adminhtml/system_config_source_yesno</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>
            				</ENABLED>
          				</fields>
			</sample>
			</groups>
    </test>
  </sections>
</config>

>source_model< attribute tell magento from where to fetch the values for the drop down menu. Mage/Adminhtml/Model/System/Config/Source directory contains lots of useful sources already defined like “Yes/No” or “Enable/Disable” or lists of countries, currencies or languages.
If you want to create your custom source_Model, you need to do the following steps:
Create a Source.php in app/code/local/Excellence/Test/Model/Source.php Excellence_Test is our module name. You need to create a function name toOptionArray() as mention below, the value and label can be anything you want to be display in dropdown

<?php
class Excellence_Test_Model_Source 
{
  public function toOptionArray()
  {
    return array(
      array('value' => 0, 'label' =>'First item'),
      array('value' => 1, 'label' => 'Second item'),
      array('value' => 2, 'label' =>'third item'),
     // and so on...
    );
  }
}

Add this model to your system.xml

<source_model>test/source</source_model>

Only these steps are required to create a custom drop down. Rest all is handled by magento.

How to create an import csv/xml

To have an export browse button only this piece of code in system.xml can do the work. You have to paste this code in fields tag of system.xml

<export translate="label">
                            <label>Export</label>
                            <frontend_type>export</frontend_type>
                            <sort_order>5</sort_order>
                            <show_in_default>0</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                      </export>

The complete system.xml is as below

<config>
  <sections>
    <test translate="label">
      	<label>A Test Section</label>
      	<tab>general</tab>
      	<frontend_type>text</frontend_type>
     	 <sort_order>1000</sort_order>
      	<show_in_default>1</show_in_default>
      	<show_in_website>1</show_in_website>
      	<show_in_store>1</show_in_store>
			<groups>
       			 <!-- New groups go here -->
        			<SAMPLE translate="label">
          			<label>A Sample Group</label>
          			<frontend_type>text</frontend_type>
          			<sort_order>100</sort_order>
          			<show_in_default>1</show_in_default>
          			<show_in_website>1</show_in_website>
          			<show_in_store>1</show_in_store>
        				<fields>
<export translate="label">
                            	<label>Export</label>
                            	<frontend_type>export</frontend_type>
                            	<sort_order>5</sort_order>
                            	<show_in_default>0</show_in_default>
                            	<show_in_website>1</show_in_website>
                           	<show_in_store>0</show_in_store>
                       	</export>
          				</fields>
			</SAMPLE>
			</groups>
    </test>
  </sections>
</config>

Secondly to add an import browse button you have to add a following xml in system.xml

  <import translate="label">
                            <label>Import</label>
                            <frontend_type>import</frontend_type>
                            <backend_model>Module_name/Model_file</backend_model>
                            <sort_order>6</sort_order>
                            <show_in_default>0</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </import>

Now we need to define where the processing for the importing occurs, like if you are importing a csv file, where does it get saved to database. For that we have defined the backend_model i.e “test/csv”
Where “test” is my Module name and csv is a php file that I created in Model folder of my custom Module. So we will create a model: app/code/local/Excellence/Test/Model/Csv.php

class Excellence_Test_Model_Csv extends Mage_Core_Model_Config_Data
{
    public function _afterSave()
    {
        Mage::getResourceModel('test/test')->uploadAndImport($this);
    }
}

As you can see we are simply calling a function in another model.
In this Test.php you can simply write your code in the function uploadAndImport()

<?php 
class Excellence_Test_Model_Mysql4_Test extends Mage_Core_Model_Mysql4_Abstract
{
    
	public function uploadAndImport(Varien_Object $object)
    	{
		$csvFile = $_FILES['groups']['tmp_name']['SAMPLE']['fields']['import']['value'];		
/*this $csvFile have all the data from the csv file you can use you logic to extract and save in database.
	}
	Here $_FILES['groups']['tmp_name']['SAMPLE']['fields']['import']['value'];
/*
		Here ‘groups’ is for group tag in system.xml.
			‘tmp_name’ is temporary location where file is save 
			‘sample’ stands for name of group. E.g. in our system.xml SAMPLE is one group
			‘fields’ stands for field tag in system.xml.
			‘import’ stands of name of field of import browse button in system.xml
			‘value’ stands for value of file uploaded. 
*/
	$csvFile = $_FILES['groups']['tmp_name']['SAMPLE']['fields']['import']['value'];
  • sdfcsdf

    ascasdcasdasfsdf

    • sdfcsdf

      sefawfasfasf

  • koushik sadhukhan

    Hello Deepak,
    I am regular reader of your post and this site also. I have read part 8 and part 9 of these module series. But when I have put ‘test/source
    test/source’ it is showing error in magento 1.8 and 1.9. Please help me out.
    I have resolved the error any way in spite of that the source data is not coming up in admin panel.

  • Abhilash N

    This is not working