In magento in a module if you're required to create a admin panel tab in which you need to display Category Drop down field through system.xml file in your module, then lets see how we can do it.
1- In the etc folder of the module open the system.xml(i.e., at app/code/local/Your/Module/etc ) file follow the codes as described below.
2- Now in the module in model folder create a folder structure like /System/Config/Source(i.e., at app/code/local/Your/Module/Model/System/Config/Source) in this 'Source' folder create a class category.php that extends the core class Adminhtml_Model_System_Config_Source_Category and in that class follow the codes as described at step 2.
Now, lets check out the codes for the above steps :
1- In your system.xml file add this code:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<tabs>
<Your translate="label" module=module"">
<label>Your Extensions</label>
<sort_order>100</sort_order>
</Your>
</tabs>
<sections>
<Your translate="label" module="module">
<label> Module Options</label>
<tab>Your</tab>
<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>
<your_group translate="label" module="module">
<label>General</label>
<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>
<fields>
<your_field_name translate="label comment">
<label>Category: </label>
<comment>Choose category.</comment>
<frontend_type>select</frontend_type>
<source_model>Your_module/system_config_source_category</source_model>
<sort_order>2</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</your_field_name>
</fields>
</your_group>
</groups>
</Your>
</sections>
</config>
Suppose, in the section as 'your' you have a group as 'your_group' in which you need to add a field 'Category'. For that purpose in the fields tag create a field as
<module_choose_category translate="label comment">
<label>Category: </label>
<comment>Choose category.</comment>
<frontend_type>select</frontend_type>
<source_model>module/system_config_source_category</source_model>
<sort_order>2</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</module_choose_category>
In that field we have used two main important tags that are <frontend_type> and <source_model>.
The tag <frontend_type> defines the input type of the field as i have set it to "select" which is of drop down.
And the <source_model> tag contains the path of the model class with the path defined as module/system_config_source_category.
2- Now in the module in model in the class Your_Module_Model_System_Config_Source_Category add this function toOptionArray.
<?php
class Your_Module_Model_System_Config_Source_Category extends Mage_Adminhtml_Model_System_Config_Source_Category
{
public function toOptionArray($addEmpty = true)
{
$collection = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*');
$options = array();
foreach($collection as $category){
if($category->getName() != 'Root Catalog'){
$options[] = array(
'label' => $category->getName(),
'value' => $category->getId()
);
}
}
return $options;
}
}
In above code the class extends Mage_Adminhtml_Model_System_Config_Source_Category and override the function toOptionArray as above to get the categories in the $options array and return it.
In this way the category dropdown field will be created on the admin panel through your system.xml file in the your desired section.
0 Comment(s)