1

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!

2
  • put system.xml code..
    – Amit Bera
    Commented Nov 9, 2014 at 7:49
  • @AmitBera Updated the desc with system.xml
    – MagePsycho
    Commented Nov 9, 2014 at 8:31

2 Answers 2

2

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.

2
  • ,i am agree with you...
    – Amit Bera
    Commented Nov 9, 2014 at 9:12
  • what if the object $helper->getConfig() is singleton? less overhead?
    – MagePsycho
    Commented Nov 9, 2014 at 9:34
2

Why two helpers?

You could just have everything related to config in MagePsycho_Somemodule_Helper_Config and then use:

$config = Mage::helper('magepsycho_somemodule/config');
$config->isActive();

Helpers are singletons already.

1
  • The reason is that I don't want to instantiate two helpers in method. I want to use one generic helper and create necessary object using that.
    – MagePsycho
    Commented Nov 9, 2014 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.