25

I'm working with Magento 2 - beta.

But cannot find how to get the configuration values in the phtml files.

eg: I want to get my custom modules configuration values in Magento_Catalog/templates/product/list.phtml file.

Anyone know how to do this?

2

5 Answers 5

58

You can create a function for getting configuration values in your custom module's helper.

<?php
namespace Vendor\Module\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function getConfig($config_path)
    {
        return $this->scopeConfig->getValue(
            $config_path,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }
}

Then you can get the configuration values to call this function in any phtml files.

$this->helper('Vendor\Module\Helper\Data')->getConfig('section/group/field');
3
  • 1
    Thanks for pointing out that the ScopeConfigInterface is available from the abstract helper class! Saved me redefining it.
    – scrowler
    Commented Dec 15, 2015 at 22:12
  • How would this work with a default config value? I'm trying to figure out how to get the phone number set in the store config and display it in the header.phtml Commented Feb 22, 2016 at 19:05
  • @Dmitry I think you still need to inject \Magento\Framework\App\Config\ScopeConfigInterface in helper's constructor
    – fmsthird
    Commented Apr 15, 2019 at 8:02
7

Another way is as below

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$conf = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('group/field/value');
2
  • 1
    You should never use the object manager for instantiation of a model
    – Dave
    Commented Aug 23, 2016 at 8:07
  • This one here is sooo wrong.
    – medina
    Commented Oct 19, 2017 at 0:17
3
<?php
namespace Vendor\Module\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function getConfig($config_path)
    {
        return $this->scopeConfig->getValue(
            $config_path,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }
}

$this->helper('Vendor\Module\Helper\Data')->getConfig('section/group/field');
2

Correction in Rushvi's answer: its section/group/field

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$conf = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('section/group/field');

Also if you want to add store filter for example want to display text if save at store configuration level then just add following:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORES;
$conf = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('carriers/freeshipping/extra_info',$storeScope);

Secondly for critiques: its v good to include in your block class's constructor via DI. Here is just concept

3
  • Thanks @hassan for this sollution Commented Oct 8, 2020 at 11:44
  • 1
    Don’t use the object manager.
    – bfontaine
    Commented Jan 18, 2022 at 15:58
  • oh really, thanks. Do you know what magento use behind the seen when you inject it ? Commented Jan 18, 2022 at 18:30
2

In template (phtml) files, I use in the following way:

$config = $block->getLayout()->createBlock(\Magento\Config\Block\System\Config\Form::class);
$configValue = $config->getConfigValue('web/secure/base_url');
2
  • 1
    Nice way to avoid creating a new helper, or in cases where there is no specific module.
    – jiheison
    Commented Sep 16, 2020 at 23:50
  • 1
    This is not proper way how to get any values in PHTML. Even this is doable, you should still use block class or view model.
    – tomis
    Commented Sep 20, 2020 at 8:47

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.