Fields added through the system.xml and default values populated with config.xml, how do we fetch the data in Magento2
4 Answers
Implementing in a class,
class Dummy { /** * @var \Magento\Framework\App\Config\ScopeConfigInterface */ protected $scopeConfig; /** * Recipient email config path */ const XML_PATH_EMAIL_RECIPIENT = 'contact/email/recipient_email'; public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig) { $this->scopeConfig = $scopeConfig; } /** * Sample function returning config value **/ public function getReceipentEmail() { $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE; return $this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope); } } Hope this is helpful!
- 1public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig) -- there should be "\" preceding "Magento"Reena Parekh– Reena Parekh2015-12-23 11:59:50 +00:00Commented Dec 23, 2015 at 11:59
- 2Just a hint: if you define methods which access config values in an helper class (extending
\Magento\Framework\App\Helper\AbstractHelper) you will already have the$this->scopeConfigobject without any need to inject itAlessandro Ronchi– Alessandro Ronchi2016-06-29 16:45:58 +00:00Commented Jun 29, 2016 at 16:45 - @AlessandroRonchi is there a list of all objects available in extended abstract helper classes somewhere?paj– paj2017-07-20 16:29:00 +00:00Commented Jul 20, 2017 at 16:29
- 1@paj as far as I know, you have to recursively look at constructor's dependencies of super classes;Alessandro Ronchi– Alessandro Ronchi2017-07-23 09:18:50 +00:00Commented Jul 23, 2017 at 9:18
Create a function for getting configuration values in your custom module's helper.
public function getConfig($config_path) { return $this->scopeConfig->getValue( $config_path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); } and call anywhere you want for example in test.phtml
$moduleStatus = $this->helper('Customvendorname\Custommodulename\Helper\Data')->getConfig('sectionid/groupid/fieldid'); In block and helper call like this:
$this->_objectManager->create('Customvendorname\Custommodulename\Helper\Data')->getConfig('sectionid/groupid/fieldid'); Note: construct Object Manager on class constructor or directly use as
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $objectManager->create('Customvendorname\Custommodulename\Helper\Data')->getConfig('sectionid/groupid/fieldid'); you can use the following code to get the value from the core_config table
Edit:
Create an instance of ScopeConfigInterface class using object manager
$scopeConfig = $this->_objectManager->create('Magento\Framework\App\Config\ScopeConfigInterface'); Then By using the scopeConfig object get the config value
$configPath = $sectionId.'/'.$groupId.'/'.$fieldId; $value = $scopeConfig->getValue( $configPath, \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); - 1even if it works, the use of objectManager to instantiate classes is highly discouraged. The correct approach is to use dependency injection.Marius– Marius2015-10-27 13:14:03 +00:00Commented Oct 27, 2015 at 13:14
- i guess that decreases the chance of a rewrite.huzefam– huzefam2015-10-27 13:16:13 +00:00Commented Oct 27, 2015 at 13:16
- @Marius - why is magento using $this->_objectManager->create('Magento\Catalog\Model\Category') in magento2/app/code/Magento/Catalog/Controller/Adminhtml/Category.phphuzefam– huzefam2015-10-28 10:07:33 +00:00Commented Oct 28, 2015 at 10:07
- That one didn't get refactored yet. Check this for more details magento.stackexchange.com/q/28617/146Marius– Marius2015-10-28 11:12:19 +00:00Commented Oct 28, 2015 at 11:12
-
I have done like this, I have created a helper function in Data.php file
public function getConfig($config_path) { return $this->scopeConfig->getValue( $config_path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); } Then i have called one single line in the phtml file.
$required_loc = $this->helper('Namespace\Modulename\Helper\Data')->getConfig('sectionid/groupid/fieldid');