In first tutorial we introduce the creation of system configuration , in this course we explain how set and get the value (play with configuration).
1 – Set Value Configuration :
You can set the value of specific field inside stores configuration , by using the interface \Magento\Framework\App\Config\MutableScopeConfigInterface , (magento2 by calling this interface with technique of preference give you as result a MutableScopeConfig class instance)
inject the class to controller or helper or block or anywhere :
/** * Config configResource * * @var configResource */ protected $configResource; /** * @param Context $context * @param MutableScopeConfigInterface $confige */ public function __construct( Context $context, \Magento\Framework\Json\Helper\Data $jsonHelper, \ Magento\Framework\App\Config\MutableScopeConfigInterface $config ) { $this->config = $config; parent::__construct($context); }
And you can use the function saveConfig :
$this->config->setValue('ibnab_asterisk_control/apioptions/api_key', $api_key,\Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,0);
'ibnab_asterisk_control/apioptions/api_key' : the path to you field configuration as (section/group/id)
$api_key : is the value to set
\Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT : the scope to use to determine config value, e.g., 'store' or 'default' , here we use the const SCOPE_TYPE_DEFAULT = 'default';
0 : the scopeCode
<preference for="Magento\Framework\App\Config\MutableScopeConfigInterface" type="Magento\Framework\App\MutableScopeConfig" />
2 – Get Value Configuration :
You can get the value of specific field inside stores configuration , by using the interface \Magento\Framework\App\Config\ScopeConfigInterface , yes DI give you the real class responsible
Magento\Framework\App\Config :
/** * Config configResource * * @var configResource */ protected $configResource; /** * @param Context $context * @param ScopeConfigInterface $confige */ public function __construct( Context $context, \Magento\Framework\Json\Helper\Data $jsonHelper, \ Magento\Framework\App\Config\ScopeConfigInterface $config ) { $this->config = $config; parent::__construct($context); }
$this->apiKey = $this->$config->getValue('ibnab_asterisk_control/apioptions/api_key', \Magento\Store\Model\ScopeInterface::SCOPE_STORE,$scopeCode);
<preference for="Magento\Framework\App\Config\ScopeConfigInterface" type="Magento\Framework\App\Config" />
almost the same technique .
3 – Other thinks:
The module Magento_Config is a big player , magento2 in many place use \Magento\Config\Model\ResourceModel\Config for saving the configuration , is the resource responsible of saving getting configuration value from db you can use directly :
/* *var configResource */ protected $configResource; */ public function __construct( Context $context, \Magento\Config\Model\ResourceModel\Config $configResource ) { $this->configResource = $configResource; parent::__construct($context); }
and you can use :
$this->configResource->saveConfig('ibnab_asterisk_control/apioptions/api_key', $api_key,ScopeConfigInterface::SCOPE_TYPE_DEFAULT,$scopeId);
and you can use the method deleteConfig($path, $scope, $scopeId):
$this->configResource->deleteConfig('ibnab_asterisk_control/apioptions/api_key', ScopeConfigInterface::SCOPE_TYPE_DEFAULT,$scopeId);
for deleting the the row from db...
4 – About scopes :
you can use \Magento\Store\Model\ScopeInterface for getting other scope values for example:
\Magento\Store\Model\ScopeInterface::SCOPE_STORE the result is store.
and have other constants :
const SCOPE_STORES = 'stores'; const SCOPE_WEBSITES = 'websites'; const SCOPE_STORE = 'store'; const SCOPE_GROUP = 'group'; const SCOPE_WEBSITE = 'website';
or for default:
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
5 – Getting current scope and scope code (id) from URL:
inject \Magento\Framework\App\RequestInterface $request and \Magento\Store\Model\StoreManagerInterface $storeManager with di :
/** * @var \Magento\Store\Model\StoreManagerInterface */ protected $storeManager; /** * @var \Magento\Framework\App\RequestInterface */ protected $request; public function __construct( \Magento\Framework\App\Helper\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\App\RequestInterface $request ) { $this->storeManager = $storeManager; $this->request = $request; parent::__construct($context); }
and use inside getCurrentScope() for example :
public function getCurrentScope() { $scopeType = \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT; $scopeCode = 0; if($this->request->getParam(\Magento\Store\Model\ScopeInterface::SCOPE_STORE)) { $scopeType = ScopeInterface::SCOPE_STORE; $scopeCode = $this->storeManager->getStore($this->request->getParam(\Magento\Store\Model\ScopeInterface::SCOPE_STORE))->getCode(); }elseif($this->request->getParam(\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE)) { $scopeType = ScopeInterface::SCOPE_WEBSITE; $scopeCode = $this->storeManager->getWebsite($this->request->getParam(\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE))->getCode(); } else { $scopeType = \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT; $scopeCode = 0; } return [$scopeType,$scopeCode]; }
Here we get the current scope and cuurent scope code (you can get the id) , you can use this params for getting and setting the value of cuurent scope :
$arrayCurrentScope = $this->getCurrentScope(); $this->apiKey = $this->$config->getValue('ibnab_asterisk_control/apioptions/api_key', $arrayCurrentScope[0],$arrayCurrentScope[1]);
Comments