Magento – Managing System Configuration data in helper class (DRY Principle)

configurationhelpersystem.xml

Just wondering how you guys code helper class for managing system configuration data for your module.
May be this is opinion based but it's worth sharing the way of coding.

I generally create a Config.php helper class which extends from Data.php.
Dummy code looks like
app/code/local/MagePsycho/Somemodule/Helper/Config.php

<?php
class MagePsycho_Somemodule_Helper_Config extends MagePsycho_Somemodule_Helper_Data
{
    /* here define all the system configuration fields without section name i.e. group/field */
    const XML_PATH_ACTIVE       = 'option/active';
    const XML_PATH_ENABLE_LOG   = 'option/enable_log';
    /* and so on ... */

    /* here get config value using const */
    public function isActive($storeId = null)
    {
        return $this->getConfigValue(self::XML_PATH_ACTIVE, $storeId);
    }

    public function isLogEnabled($storeId = null)
    {
        return $this->getConfigValue(self::XML_PATH_ENABLE_LOG, $storeId);
    }
    /* and so on ... */
}

app/code/local/MagePsycho/Somemodule/Helper/Data.php

<?php
class MagePsycho_Somemodule_Helper_Data extends Mage_Core_Helper_Abstract
{
    const MODULE_NAMESPACE_ALIAS = 'magepsycho_somemodule'; //your helper module alias and system.xml section node

    public function getConfig()
    {
        return Mage::helper(self::MODULE_NAMESPACE_ALIAS . '/config');
    }

    public function getConfigValue($xmlPath, $storeId = null)
    {
        return Mage::getStoreConfig(self::MODULE_NAMESPACE_ALIAS . '/' . $xmlPath, $storeId);
    }
}

app/code/local/MagePsycho/Somemodule/etc/system.xml

<?xml version="1.0"?>
<config>
    <sections>
        <magepsycho_somemodule module="magepsycho_somemodule">
            <label>Some Module Info</label>
            <tab>magepsycho</tab>
            <frontend_type>text</frontend_type>
            <sort_order>900</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                <option translate="label">
                    <label>General Settings</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>
                        <active translate="label">
                            <label>Module Enabled</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>
                        </active>
                        <enable_log translate="label">
                            <label>Log Enabled</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <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>
                        </enable_log>
                        <!-- ... -->
                    </fields>
                </option>
                <!-- ... -->
            </groups>
        </magepsycho_somemodule>
    </sections>
</config>

Usage

$helper = Mage::helper('magepsycho_somemodule');
$isActive = $helper->getConfig()->isActive();
$isLogEnabled = $helper->getConfig()->isLogEnabled();

Using this approach:

  • You can easily move the fields to any groups (by just editing Config.php const values)
  • Looks more DRY
  • Isolates system config data in one place

Any room for improvement?
Please share your idea!

Best Answer

The approach you have suggested is good one thing I would add is simply a note.

If the config item is only being used in one place the do not add it to the helper simply add it to the model that needs it.

In your approach you need two helpers for one config value which could be seen as a bit of overkill. Even if there is little or no performance implications with this approach it does appear to be complex for simply loading config values.

Related Topic