3

I'm creating an extension that exposes an API to another web site. My extension will create an API key, and then you'll copy this API key into the other web site, which will let Magento and the other site exchange data.

I've created a system.xml entry for my API key. I'd like for the default value for this config item to be populated by a call to a generateApiKey method defined in PHP code.

In pseudocode, config.xml:

<config>
    <default>
       <my_extension>
            <api_connection>
                <apikey><?php echo $obj->generateApiKey(); ?></apikey>
            </api_connection>
        </my_extension>
    </default>
</config>

I know that that's not possible, but hopefully that conveys what I want to do.

How can I programmatically generate (and save) a default value for something defined in system.xml?

2 Answers 2

3

I'd suggest populating the value via a data migration script. Create a resource in your module's config.xml:

<config>
    <modules>
        <my_namespace_my_extension>
            <version>1.0.0</version>
        </my_namespace_my_extension>
    </modules>
    <global>
        <resources>
            <my_namespace_my_extension>
                <setup>
                    <module>MyNamespace_MyModule</module>
                    <class>Mage_Core_Model_Resource_Setup</class>
                </setup>
            </my_namespace_my_extension>
        </resources>
    </global>

Create a the migration script: MyNamespace/MyExtension/data/my_namespace_my_extension/data-install-1.0.0.php

/* @var $this Mage_Core_Model_Resource_Setup */
$generatedKey = Mage::helper('my_extension')->generateApiKey();
$this->setConfigData('my_extension/api_connection/apikey', $generatedKey);

NOTE: the cache would most likely need to be cleared after installation. This should be made clear in your module's installation instructions. It should also be possible to flag the cache as invalidated.

Mage::app()->getCacheInstance()->invalidateType('config');
4
  • Is that going to work? The setConfigData method will set the node value, but it won't persist the value to a database anywhere. Mar 10, 2014 at 18:59
  • 1
    It should, checkout the source here. Are you referring to Mage::getConfig()->setNode() perhaps?
    – beeplogic
    Mar 10, 2014 at 19:02
  • Should this install script have the usual $installer->startSetup() and $installer->endSetup() calls?
    – skyler
    Mar 10, 2014 at 19:11
  • It's not necessary, the "data" scripts are meant for populating data. The start/end setup methods concern themselves with how the database behaves (e.g. turning off foreign key constraints).
    – beeplogic
    Mar 10, 2014 at 19:19
2

You can just save it into core_config_data.

So either in $setup->setConfigData($path, $value) or Mage::app()->getConfig()->saveConfig($path, $value) but be careful, the second one doesn't clean the cache.

Your Answer

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

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