Magento2 -How to get data from system configuration

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!


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
    );