1

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.

0

1 Answer 1

1

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

5
  • 1
    Unfortunately, I tried and the result is the same : it returns a correct value for front call but incorrect in admin. Sep 15, 2019 at 10:55
  • I think this is default Magento feature to get the value from the store view if it is different from the default view. If you want both of the values in your code for default and store view, then you can pass the store on conditional basis in your helper as per your requirement.
    – Sumit
    Sep 15, 2019 at 12:06
  • I've updated my code as per your requirement, please have a review.
    – Sumit
    Sep 15, 2019 at 12:15
  • 2
    Thanks for your message, I will try it asap. I agree with your sentence : I think this is default Magento feature to get the value from the store view if it is different from the default view. and that's the problem I'm pointing out : when I am from the admin, retrieving a value with a store scope should return the default config : because the admin store is the default store (in my Magento 1 way of thinking). The current result (with my code) is not the expected result. Now I will try your updated code ! Sep 15, 2019 at 12:28
  • 1
    your logic is working. I will upvote your answer (I will flag it as the accepted answer later if there is no other acceptable proposition). I will edit my question too with an updated code using your answer's logic. Sep 15, 2019 at 12:51

Your Answer

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

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