I want to use 2 tables in database for system.xml in my module as toOptionArray
only for adminhtml config, how can I use it?
it work same as origin country and state ins shipping settings but want use my own table and they are states and cities, means when I select region from dropdown then cities of this state load and I can select one of them
Created 2 table in databse with the name of directory_region
and directory_region_city
and inserted region_id
, region_name
, code
, city_id
, city_name
in these 2 tables
Best Answer
When defining your field in your
system.xml
file you can specify thesource_model
of this field. For example the source model for the standard yes no drop down is as follows.This will tell the field to use the model
Mage_Adminhtml_Model_System_Config_Source_Yesno
for building the options. Inside this model you will see the functiontoOptionArray
whichSo using this as a basis for your requirements what you can do is set-up your own model for example
Your_Module_Model_System_Config_Region
, then you would simply need to use your table's collection to build the correct structure in the functiontoOptionArray
.As far as loading one source model based on the value of another you have two options.
Using Frontend Models
Perfect example of this is the tax options for country and region under
app/code/core/Mage/Tax/etc/system.xml
.Basically what you need to do is create a frontend_model that extends
Mage_Adminhtml_Block_System_Config_Form_Field
. This block will only need to have the function_getElementHtml
. What you can do then is call the parent function so the default select is created and show and then add some javascript that will update this select based on when another element has changed.In the tax example it uses the standard
RegionUpdater
but it is possible to work with the more genericSelectUpdater
which allows for one select element to change another.Now I will show you in depth how this can be achieved. In this example I am using the magento standard country and region collections but you can easily plug in your own collections when needed.
Firstly you will need the system.xml, this should add the correct
frontend_model
andsource_model
references:For this example the two source_models are the same as the standard
Mage_Tax
source models so there is nothing special there. But you could here load all the options you need for from your tables.The next thing of importance is the
frontend_model
here what you do is extend the classMage_Adminhtml_Block_System_Config_Form_Field
and create your own_getElementHtml
function. This function will call the parent_getElementHtml
and set a variable with the returned html. Then you can build your javascript using theSelectUpdater
you can see from the init, in filejs/mage/adminhtml/form.js
, that you need to specify the following.My example looks as follows.
The values are built from my helper and replaced into the html string just before being returned. The easiest way to build the values is to create an array of the following format.
Here is my example's helper for building the values, but you could easily extend it to use what ever collections you need for your need.