Question

In Magento 1, a call to Mage::getStoreConfig('section/group/field') was able to retrieve a config of the current store. Indeed, if we called it from a front block for example, it retrieved the config value of the current store and if we called it from an admin block, it retrieved the value of the admin store (id=0).

Is it still possible in Magento 2 to have such a method ?

For example, I created an helper with the following content :

namespace VendorName\ModuleName\Helper;

use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;

class Config extends AbstractHelper
{
    const XML_PATH_FEATURE_ENABLED = 'section/group/my_feature_enabled';

    /**
     * Get enabled config
     * @param int|null $storeId
     * @return mixed
     */
    public function isEnabled($storeId = null)
    {     
            return $this->scopeConfig->getValue(
                self::XML_PATH_FEATURE_ENABLED,
                ScopeInterface::SCOPE_STORE,
                $storeId
            );
    }
}

For informations : in my test environnement, I have only one store view. My core_config_data table looks like :

scope   |  scope_id |                path               |    value 
default |    0      | section/group/my_feature_enabled  |      0
stores  |    1      | section/group/my_feature_enabled  |      1

When I call it from the frontend area, it works as expected (returns 1). But when I call it from the admin, it does not return the default admin value : it returns 1 too ...

If I change ScopeInterface::SCOPE_STORE by default in the isEnabled method, it always returns the default value (admin value) but it's not returning the good front end value anymore ...

So, what is the Magento 2 way to have the same result as the Magento 1 Mage::getStoreConfig method ?

UPDATE :

Using the answer of @Sumit, I was able to create a method working as I expected :

namespace VendorName\ModuleName\Helper;

use Magento\Framework\App\Area;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\State;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;

class Config extends AbstractHelper
{
    const XML_PATH_FEATURE_ENABLED = 'section/group/my_feature_enabled';

    /**
     * @var State
     */
    protected $_state;
    /**
     *
     * @var bool
     */
    protected $_isEnabled;

    public function __construct(
        State $state,
        Context $context
    ) {
        parent::__construct($context);
        $this->_state = $state;
    }
    /**
     * Get enabled config
     * @param int|null $storeId
     * @return mixed
     */
    public function isEnabled($storeId = null)
    {     
       $scopeCode = ($this->_state->getAreaCode() == Area::AREA_ADMINHTML) ?
                    ScopeConfigInterface::SCOPE_TYPE_DEFAULT :
                    ScopeInterface::SCOPE_STORE;
       return $this->scopeConfig->getValue(
                self::XML_PATH_FEATURE_ENABLED,
                $scopeCode,
                $storeId
              );

    }
}

If there is other suggestion, please share with us.

Was it helpful?

Solution

Give a try with below code.

protected $_scopeConfig;
protected $_areaCode;

public function __construct( 
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Framework\App\State $areaCode
) 
{
    $this->_scopeConfig = $scopeConfig;
    $this->_areaCode = $areaCode;
}

public function getYourValue() {
    if($this->_areaCode->getAreaCode() == \Magento\Framework\App\Area::AREA_ADMINHTML) {
         //if area is admin
    } else {
        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORES;
        $this->_scopeConfig->getValue("section_id/group_id/field_id", $storeScope);
    }
}

Reference: How to get admin config values from a specific store view

Hope it helps!!!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top