/   /   /  Magento 2 Some techniques about system configuration(part2)

Note:

For more extensions and themes visit our store

Magento 2 Some techniques about system configuration(part2)

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 :

  1.  
  2.     /**
  3.      * Config configResource
  4.      *
  5.      * @var configResource
  6.      */
  7.    protected $configResource;
  8.   /**
  9.    * @param Context $context
  10.    * @param MutableScopeConfigInterface  $confige
  11.      */
  12.     public function __construct(
  13.         Context $context,
  14.         \Magento\Framework\Json\Helper\Data $jsonHelper,
  15.         \ Magento\Framework\App\Config\MutableScopeConfigInterface $config
  16.         
  17.     ) {
  18.         $this->config  = $config;
  19.         parent::__construct($context);
  20.     }
  21.  
  22.  
 

 And you can use the function saveConfig :

  1.  
  2. $this->config->setValue('ibnab_asterisk_control/apioptions/api_key', $api_key,\Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,0);
  3.  
  4.  

'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  :

  1.  
  2.     /**
  3.      * Config configResource
  4.      *
  5.      * @var configResource
  6.      */
  7.    protected $configResource;
  8.    /**
  9.    * @param Context $context
  10.    * @param ScopeConfigInterface   $confige
  11.      */
  12.     public function __construct(
  13.         Context $context,
  14.         \Magento\Framework\Json\Helper\Data $jsonHelper,
  15.         \ Magento\Framework\App\Config\ScopeConfigInterface  $config
  16.         
  17.     ) {
  18.         $this->config  = $config;
  19.         parent::__construct($context);
  20.     }
  21.  
  22.  
 

  1.  
  2.    $this->apiKey = $this->$config->getValue('ibnab_asterisk_control/apioptions/api_key', \Magento\Store\Model\ScopeInterface::SCOPE_STORE,$scopeCode);
  3.  
  4.  

    <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 :

  1.  
  2.     /*
  3.      *var configResource
  4.      */
  5.     protected $configResource;
  6.     */
  7.     public function __construct(
  8.         Context $context,
  9.         \Magento\Config\Model\ResourceModel\Config $configResource
  10.          
  11.     ) {
  12.         $this->configResource  = $configResource;
  13.         parent::__construct($context);
  14.     }
  15.  
  16.  
 
 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 :

  1.  
  2.     const SCOPE_STORES = 'stores';
  3.     const SCOPE_WEBSITES = 'websites';
  4.     const SCOPE_STORE   = 'store';
  5.     const SCOPE_GROUP   = 'group';
  6.     const SCOPE_WEBSITE = 'website';
  7.  
  8.  
 

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 :

  1.  
  2.     /**
  3.      * @var \Magento\Store\Model\StoreManagerInterface
  4.      */
  5.     protected $storeManager;
  6.     /**
  7.      * @var \Magento\Framework\App\RequestInterface
  8.      */
  9.     protected $request; 
  10.     public function __construct(
  11.         \Magento\Framework\App\Helper\Context $context,
  12.         \Magento\Store\Model\StoreManagerInterface $storeManager,
  13.         \Magento\Framework\App\RequestInterface $request
  14.     ) {
  15.         $this->storeManager = $storeManager;
  16.         $this->request = $request;
  17.         parent::__construct($context);
  18.     }
  19.  
  20.  

and use inside getCurrentScope() for example :

  1.  
  2.     public function getCurrentScope()
  3.     {
  4.        $scopeType = \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
  5.        $scopeCode = 0;
  6.        if($this->request->getParam(\Magento\Store\Model\ScopeInterface::SCOPE_STORE))
  7.        {   
  8.         $scopeType = ScopeInterface::SCOPE_STORE;
  9.         $scopeCode = $this->storeManager->getStore($this->request->getParam(\Magento\Store\Model\ScopeInterface::SCOPE_STORE))->getCode();
  10.        }elseif($this->request->getParam(\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE))
  11.        {
  12.         $scopeType = ScopeInterface::SCOPE_WEBSITE;
  13.         $scopeCode = $this->storeManager->getWebsite($this->request->getParam(\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE))->getCode();
  14.        }   
  15.        else
  16.        {
  17.         $scopeType = \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
  18.         $scopeCode = 0;       
  19.        }
  20.        return [$scopeType,$scopeCode];
  21.     }
  22.  
  23.  

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 :

  1.  
  2. $arrayCurrentScope = $this->getCurrentScope();
  3.    $this->apiKey = $this->$config->getValue('ibnab_asterisk_control/apioptions/api_key', $arrayCurrentScope[0],$arrayCurrentScope[1]);
  4.  
  5.  

Comments

IBNAB is a company made of a group of professionals whose work is providing secure open source solutions. Our company strives for reaching magnificent results with each experience and provides professional open source solutions that cover every part of the business process.

Newsletter