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?
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?
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');
\Magento\Framework\App\Config\ScopeConfigInterface
in helper's constructor
Another way is as below
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$conf = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('group/field/value');
<?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');
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
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');