1

Is there a way to do some action at module registration/installation?

It can be through event observer or through migration.

I want to call a function to get some values and store them in core_config_data during installation.

Thanks.

0

You can use InstallData.php to store value in core_config_data during installation

use below code for example :

$configdata = [
                'scope' => DEFINE_YOUR_SCOPE
                'scope_id' => 0,
                'path' => DEFINE_YOUR_PATH
                'value' => YOUR_VALUE,
            ];
 $setup->getConnection()->insertOnDuplicate($setup->getTable('core_config_data'), $configdata, ['value']);
  • Thanks @Hariharan. I want to also do some action (call an api using this config values as parameters) during installation. Can you help? – subhas Aug 20 '18 at 11:46
  • Not sure. you can try to create a new function to call api for testing in InstallData.php and call into install function – Hariharan Aug 21 '18 at 5:27
0

Try to save config values via config.xml

First Method:

app/code/VendorName/ModuleName/etc/config.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <sectionName>
            <groupName>
                <fieldName>add value here</fieldName>
            </groupName>
        </sectionName>
    </default>
</config>

For eg.

<default>
    <payment>
        <checkmo>
            <active>1</active>
            <order_status>pending</order_status>
            <title>Check / Money order</title>
        </checkmo>
    </payment>
</default>

Second Method:

<?php
namespace VendorName\ModuleName\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
 * @codeCoverageIgnore
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class InstallData implements InstallDataInterface
{
   public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
   {

        $data = [
            'scope' => 'default',
            'scope_id' => 0,
            'path' => "{section-name}/{group-name}/{field-name}",
            'value' => "Value",
        ];
        $setup->getConnection()
            ->insertOnDuplicate($setup->getTable('core_config_data'), $data, ['value']);
    }
}

After run below commands:

  • php bin/magento setup:upgrade
  • php bin/magento setup:di:compile
  • php bin/magento cache:clean
  • Thanks @Abdul. The idea is to collect some store information and store them into the config data and use them as a parameter to call an api during module installation. Is this possilbe? – subhas Aug 20 '18 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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