Magento Expert Forum - Improve your Magento experience

Results 1 to 2 of 2

Magento Module Development - Part 6 - System.xml Advance and Module

  1. #1
    Junior Member rocker's Avatar
    Join Date
    Mar 2013
    Posts
    105
    Thanks
    3
    Thanked 11 Times in 9 Posts

    Cool Magento Module Development - Part 6 - System.xml Advance and Module

    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 Part 5

    Custom Dropdown in system.xml

    Below is the system.xml for a drop down

    HTML Code:
    <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><!&#91;CDATA&#91;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 Code:
    <?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

    HTML Code:
    <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

    HTML Code:
    <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

    HTML Code:
    <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

    HTML Code:
    <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

    PHP Code:
    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 Code:
    <?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'];

    View more threads in the same category:


  2. #2
    Junior Member
    Join Date
    May 2013
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for sharing this.

Similar Threads

  1. Magento Module Development - Part 4 - Database and SQL
    By rocker in forum Programming & Development
    Replies: 4
    Last Post: 19-01-2021, 09:16 AM
  2. Magento Module Development - Part 9 - Collection and SQL
    By rocker in forum Programming & Development
    Replies: 9
    Last Post: 21-08-2018, 01:48 PM
  3. Magento Module Development - Part 8 - Events
    By rocker in forum Programming & Development
    Replies: 0
    Last Post: 29-04-2013, 02:46 AM
  4. Magento Module Development - Part 7 - Overriding
    By rocker in forum Programming & Development
    Replies: 0
    Last Post: 29-04-2013, 02:37 AM
  5. Magento Module Development - Part 5 - System.xml and Module
    By rocker in forum Programming & Development
    Replies: 0
    Last Post: 29-04-2013, 02:25 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •