Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get magento backend config xml data?

I have a system.xml in my module, which starts with this:

<config>
    <sections>
        <dev>
            <groups>
                <my_module>
                    <label>...

I want to get the value of this label, from a different module. How do I do it? My first thought, was Mage::getConfig('sections/dev/groups/my_module/label'), but this doesn't work - it seems the <sections> area of the config is not accessible. I also can't figure out where magento is loading this value, which it must do at some point, or it wouldn't be able to display it.

To be completely clear: I am not trying to get the config data value as stored in the core_config_data table, that's no trouble. I want to be able to get the other attributes relating to it - like the group label, or the sort order of the fields, and to do that I need to be able to read the <sections> area of the config.

like image 223
Benubird Avatar asked Apr 09 '13 15:04

Benubird


People also ask

Where is config xml in Magento?

For example, the configuration object for config. xml is Magento\Framework\App\Config.

How can you fetch a system configuration value programmatically?

In order to fetch system store configuration values by scope level on store or website, you will be required to use ScopeConfigInterface with getValue() method. The first argument sectionId/groupId/fieldId is from your etc/adminhtml/system. xml file. The second argument will be the scope value.

In which table custom configuration are stored in Magento?

In this post we will show you how to add custom system configuration settings in Magento 2. System configuration values in Magento 2 are stored in the core_config_data database table, which is exactly the same as in Magento 1.


2 Answers

As so often seems to be the case, I find the answer moments after posting the question...

This is how to get sections/dev/my_module/label:

Mage::getSingleton('adminhtml/config')->getSection('dev')->groups->my_module->label

As you can see, you need to use Mage::getSingleton('adminhtml/config')->getSection('dev') to get the backend config (you can also use ->getSections() to get all the sections to iterate over). This returns a Mage_Core_Model_Config_Element Object, which is the root of a tree of objects, accessible as shown. Just do a print_r at any stage and you'll see the rest of the tree, which print_r formats like an array, although it's not.

like image 41
Benubird Avatar answered Oct 02 '22 05:10

Benubird


The system.xml files are never merged with the global configuration. They're only loaded when Magento builds the user interface for the

System -> Configuration 

section of the backend admin application. Other than that the application has no use for them.

If you want to grab the label, you'll need to load the full system.xml configuration yourself. Something like this should work.

//load and merge `system.xml` files
$config = Mage::getConfig()->loadModulesConfiguration('system.xml');        

//grab entire <sections/> node
var_dump($config->getNode('sections')->asXml());        

//grab label from a specific option group as a string
var_dump((string)$config->getNode('sections/dev/groups/restrict/label'));

As mentioned in another answer in this thread, there's also an adminhtml/config model class which wraps some of this logic in a getSection method, so you could do something like this.

Mage::getSingleton('adminhtml/config')->getSection('dev')->groups->my_module->label

If you look at the source of getSection

#File: app/code/core/Mage/Adminhtml/Model/Config.php
public function getSections($sectionCode=null, $websiteCode=null, $storeCode=null)
{
    if (empty($this->_sections)) {
        $this->_initSectionsAndTabs();
    }

    return $this->_sections;
}

and follow the call stack through to _initSectionsAndTabs

#File: app/code/core/Mage/Adminhtml/Model/Config.php
protected function _initSectionsAndTabs()
{
    $config = Mage::getConfig()->loadModulesConfiguration('system.xml')
        ->applyExtends();

    Mage::dispatchEvent('adminhtml_init_system_config', array('config' => $config));
    $this->_sections = $config->getNode('sections');
    $this->_tabs = $config->getNode('tabs');
}

You'll see this wrapper method eventually calls the loadModulesConfiguration method itself. The additional applyExtends if an old bit of meta-programming in the configuration you can read about here, which is part of a longer series on configuration loading. (self-links, too long for a StackOverflow answer).

The reason I personally wouldn't use this to grab values out of the configuration is the event that's dispatched when you make this call

Mage::dispatchEvent('adminhtml_init_system_config', array('config' => $config));

This event may trigger code in your system that assumes you're loading the system configuration system in the backend admin console area. If you just want to read the XML tree. simply loading it yourself and reading the values seems the way to go. Your use case, of course, may vary.

like image 100
Alan Storm Avatar answered Oct 02 '22 05:10

Alan Storm