Tuesday, February 12, 2013

Create system config section with dynamic number of fields

Here is how you can create a section in System->Configuration with dynamic number of fields.
The following example will create a config section with yes/no dropdowns for each customer group.
For this you need to create a new module. Let's call it Easylife_Configsection. You can change the name to whatever you want but make sure you change the file names and references in the code.
The extension has the following files: app/etc/modules/Easylife_Configsection.xml - module declaration file
<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Configsection>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
             <Mage_Adminhtml/>
            </depends>
        </Easylife_Configsection>
    </modules>
</config>
app/code/local/Easylife/Configsection/etc/config.xml - module config file
<?xml version="1.0"?>
<config>
 <modules>
  <Easylife_Configsection>
   <version>1.0.0</version>
  </Easylife_Configsection>
 </modules>
 <global>
  <blocks>
   <configsection>
    <class>Easylife_Configsection_Block</class>
   </configsection>
  </blocks>
  <helpers>
   <configsection>
    <class>Easylife_Configsection_Helper</class>
   </configsection>
  </helpers>
 </global>
</config>
app/code/local/Easylife/Configsection/Helper/Data.php - the module main helper
<?php 
class Easylife_Configsection_Helper_Data extends Mage_Core_Helper_Abstract{
 
}
app/code/local/Easylife/Configsection/etc/system.xml - this will add the section in the config area
<?xml version="1.0"?>
<config>
 <sections>
  <customer_groups_demo translate="label" module="configsection"><!-- this ads a new section in the config you can change the tab name to what you need-->
   <label>Customer groups demo</label>
            <tab>customer</tab><!-- the section is added in the customer tab. you can change to anything-->
            <frontend_type>text</frontend_type>
            <sort_order>200</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
   <groups>
    <demo translate="label" module="configsection"><-- this add a group in the section you just created change the name to whatever you need -->
                    <label>Demo customer groups</label>
                    <frontend_type>text</frontend_type>
                    <frontend_model>configsection/adminhtml_system_config_form_fieldset_customer_groups</frontend_model><-- this is the model that renders all the fields -->
                    <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>
                </demo> 
   </groups>
  </customer_groups_demo>
 </sections>
</config>
app/code/local/Easylife/Configsection/etc/adminhtml.xml - this will add the acl for you section
<?xml version="1.0"?>
<config>
    <acl>
        <resources>
            <admin>
                <children>
                    <system>
                        <children>
                            <config>
                                <children>
                                    <customer_groups_demo translate="title" module="configsection"><-- this tag sould be named as the section you create -->
                                        <title>Customer groups demo</title>
                                    </customer_groups_demo>
                                </children>
                            </config>
                        </children>
                    </system>
                </children>
            </admin>
        </resources>
    </acl>
</config>
Now the fun part. app/code/local/Easylife/Configsection/Block/Adminhtml/System/Config/Form/Fieldset/Customer/Groups.php - this is the model that create the fields in the config section
<?php 
class Easylife_Configsection_Block_Adminhtml_System_Config_Form_Fieldset_Customer_Groups extends 
 Mage_Adminhtml_Block_System_Config_Form_Fieldset{
 protected $_dummyElement;
    protected $_fieldRenderer;
    protected $_values;

    public function render(Varien_Data_Form_Element_Abstract $element)
    {
        $html = $this->_getHeaderHtml($element);
 //here you cand loop through all the fields you want to add
 //for each element you neet to call $this->_getFieldHtml($element, $group);
        $groups = Mage::getModel('customer/group')->getCollection();

        foreach ($groups as $group) {
            $html.= $this->_getFieldHtml($element, $group);
        }
        $html .= $this->_getFooterHtml($element);

        return $html;
    }
    //this creates a dummy element so you can say if your config fields are available on default and website level - you can skip this and add the scope for each element in _getFieldHtml method
    protected function _getDummyElement()
    {
        if (empty($this->_dummyElement)) {
            $this->_dummyElement = new Varien_Object(array('show_in_default'=>1, 'show_in_website'=>1));
        }
        return $this->_dummyElement;
    }
    //this sets the fields renderer. If you have a custom renderer tou can change this. 
    protected function _getFieldRenderer()
    {
        if (empty($this->_fieldRenderer)) {
            $this->_fieldRenderer = Mage::getBlockSingleton('adminhtml/system_config_form_field');
        }
        return $this->_fieldRenderer;
    }
    //this is usefull in case you need to create a config field with type dropdown or multiselect. For text and texareaa you can skip it.
    protected function _getValues()
    {
        if (empty($this->_values)) {
            $this->_values = array(
                array('label'=>Mage::helper('adminhtml')->__('No'), 'value'=>0),
                array('label'=>Mage::helper('adminhtml')->__('Yes'), 'value'=>1),
            );
        }
        return $this->_values;
    }
    //this actually gets the html for a field
    protected function _getFieldHtml($fieldset, $group)
    {
        $configData = $this->getConfigData();
        $path = 'customer_groups_demo/demo/group_'.$group->getId();//this value is composed by the section name, group name and field name. The field name must not be numerical (that's why I added 'group_' in front of it)
        if (isset($configData[$path])) {
            $data = $configData[$path];
            $inherit = false;
        } else {
            $data = (int)(string)$this->getForm()->getConfigRoot()->descend($path);
            $inherit = true;
        }

        $e = $this->_getDummyElement();//get the dummy element

        $field = $fieldset->addField($group->getId(), 'select',//this is the type of the element (can be text, textarea, select, multiselect, ...)
            array(
                'name'          => 'groups[demo][fields][group_'.$group->getId().'][value]',//this is groups[group name][fields][field name][value]
                'label'         => $group->getCustomerGroupCode(),//this is the label of the element
                'value'         => $data,//this is the current value
                'values'        => $this->_getValues(),//this is necessary if the type is select or multiselect
                'inherit'       => $inherit,
                'can_use_default_value' => $this->getForm()->canUseDefaultValue($e),//sets if it can be changed on the default level
                'can_use_website_value' => $this->getForm()->canUseWebsiteValue($e),//sets if can be changed on website level
            ))->setRenderer($this->_getFieldRenderer());

        return $field->toHtml();
    }
}
That's about it. Clear the cache, logout of the admin and login again and you should be able to use this. You can get the value of a setting just like any other magento config setting:
$groupId = 1;
$val = Mage::getStoreConfig('customer_groups_demo/demo/group_'.$groupId);//section_name/group/field
Marius.