Customizing Magento System Configuration. Part 2

From the previous article, we’ve learned how to change Magento System Configuration using the system configuration file. Today we will customize tabs and sections, groups and fields without using system.xml.

Certainly, we know that Magento has a great event system and this knowledge will help us to make changes in the System Configuration.
For our purposes, we will create a new module and call it ‘SystemConfig’. It consists of three files. First of them is for initialing our module:
app/etc/modules/Atwix_SystemConfig.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Atwix_SystemConfig>
            <active>true</active>
            <codePool>local</codePool>
        </Atwix_SystemConfig>
    </modules>
</config>

Afterwards, we need to create module configuration file. That is the second one:

app/code/local/Atwix/SystemConfig/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Atwix_SystemConfig>
            <version>1.0.0</version>
        </Atwix_SystemConfig>
    </modules>
    <global>
        <models>
            <atwix_systemconfig>
                <class>Atwix_SystemConfig_Model</class>
            </atwix_systemconfig>
        </models>
        <events>
            <adminhtml_init_system_config>
                <observers>
                    <atwix_init_system_config>
                        <class>atwix_systemconfig/observer</class>
                        <method>changeSystemConfig</method>
                    </atwix_init_system_config>
                </observers>
            </adminhtml_init_system_config>
        </events>
    </global>
</config>

However, let’s check what we have now. So, in the configuration file we define model, configure event, and also, the event ‘adminhtml_init_system_config’ which makes possible to work with System Configuration data.

And, the third  one, the last file – it is an observer, where we do all the magic:
app/code/local/Atwix/SystemConfig/Model/Observer.php

<?php

class Atwix_SystemConfig_Model_Observer
{
    public function changeSystemConfig(Varien_Event_Observer $observe)
    {
        $config = $observe->getConfig();

        return $this;
    }
}

Okey, we put debug breakpoint to the line with the return statement and have a good inspect of the picture being used:

inspect

Check this out!
In the config, there are all the tabs and sections, and in the section – groups and fields, but what about to try some changes. For example, let’s change sort order and label for ‘advanced’ tab and add a comment to the ‘Startup Page’ field.

Thuswise, at first – change code in the observer:

app/code/local/Atwix/SystemConfig/Model/Observer.php

<?php

class Atwix_SystemConfig_Model_Observer
{
    public function changeSystemConfig(Varien_Event_Observer $observer)
    {
        //get init sections and tabs
        $config = $observer->getConfig();

        //get tab 'advanced', change sort order and label
        $advancedTab = $config->getNode('tabs/advanced');
        $advancedTab->sort_order = 1;
        $advancedTab->label .= ' (on top)';

        //get field 'page', add comment
        $config->getNode('sections/admin/groups/startup/fields/page')->comment = 'after successful login you will see this page';

        return $this;
    }
}

first_example

Great, advanced tab on the top!
After what, it is possible to make a lot of changes.
Moreover, we can also create tabs and section, group and field, besides that, using Mage_Core_Model_Config_Element class we are able to work with elements: create, edit, append, extend, etc. And the next example shows us how to add a new group with the fields to the ‘admin’ section. In this case, the observer should have the following code:

app/code/local/Atwix/SystemConfig/Model/Observer.php

<?php

class Atwix_SystemConfig_Model_Observer
{
    public function changeSystemConfig(Varien_Event_Observer $observer)
    {
        //get init sections and tabs
        $config = $observer->getConfig();

        //get tab 'advanced', change sort order and label
        $advancedTab = $config->getNode('tabs/advanced');
        $advancedTab->sort_order = 1;
        $advancedTab->label .= ' (on top)';

        //get field 'page', add comment
        $config->getNode('sections/admin/groups/startup/fields/page')->comment = 'after successful login you will see this page';

        //add new group with fields in section 'admin'
        $adminSectionGroups = $config->getNode('sections/admin/groups');
        $new_group_xml = new Mage_Core_Model_Config_Element('
            <atwix_new_group>
                <label>New Group</label>
                <sort_order>99</sort_order>
                <show_in_default>1</show_in_default>
                <show_in_website>1</show_in_website>
                <show_in_store>1</show_in_store>
                <fields>
                        <disable_message>
                            <label>Disable Message</label>
                            <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>
                        </disable_message>
                        <message>
                            <label>Message</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>20</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <depends>
                                <disable_message>0</disable_message>
                            </depends>
                        </message>
                </fields>
            </atwix_new_group>
        ');
        $adminSectionGroups->appendChild($new_group_xml);

        return $this;
    }
}

second_example

But, if we wish it is possible to implement a lot of things and reinvent the wheel =)

At the same time, it will be highly helpful to make some changes in the System Configuration while using the observer.

You’re always welcome to leave your comments below if you found this post helpful! Thanks for reading our blog!