cancel
Showing results for 
Search instead for 
Did you mean: 

How to get value from admin configuration for my custom module

How to get value from admin configuration for my custom module

I have created a custom module, and added values directly in my code like hardcode, how to get those values from the admin configuration and passed to my code.

Block:

<?php
namespace Zero\Customerreview\Block;

class Customerreview extends \Magento\Framework\View\Element\Template
{
        
     public function __construct(        \Magento\Framework\View\Element\Template\Context $context,        
        array $data = []
     ) {
       
        parent::__construct($context, $data);
    }
  
    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        
    }



    public function getCustomerReview(){    

        $authorization = "Authorization: Bearer nhuhtrbhuithtobtougbtgbigbrigboirubgurbgrbgibniutgtbgbuigbuobtrbntrhtrnijhiotgnbgnbhtrhnitgnbgibnrtshgtrnirtn'jhiotrtsnnioonrtrhtitrh";

        

        $url = 'https://mybusiness.googleapis.com/v4/accounts/123456789/locations/987654321/reviews';  

        $ch = curl_init();
        //curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $output = curl_exec($ch);        
        $allData = json_decode($output, TRUE); // You will get all the data

        return $allData;
    }
}

I want to get $authorization and account id ( https://mybusiness.googleapis.com/v4/accounts/123456789)  details from admin configuration, and also how to pass that account id in URL? 

2 REPLIES 2

Re: How to get value from admin configuration for my custom module

Hi @Aveeva ,

 

Can you please add system configuration in admin and add 2 text boxes in admin which will have 2 details.(authorization and account)

https://www.tutorialsplane.com/magento-2-create-system-configuration/

 

Now after adding these 2 details in admin. You can get details in the block file like below.

https://www.hiddentechies.com/blog/magento-2/get-system-configuration-value-phtml-file/

 

 

Hope this helps you!

Problem Solved! Click Kudos & Accept as Solution!

 

Re: How to get value from admin configuration for my custom module

Hi @Aveeva,

 

We need to call the default method available. Just use \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, in your constructor argument and set the class property: $this->scopeConfig = $scopeConfig;

 

Now to get the configuration value just use

$this->scopeConfig->getValue('dev/debug/template_hints', 
    \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

Hope this can help you! Let me know if you need further assistance. 

__________

If issue solved, Click Kudos & Accept as Solution

LitExtension - #1 Shopping Cart Migration Expert

LitExtension helps store owners and agencies migrate all important data from one eCommerce platform to another accurately, securely and at the highest speed.

Visit website: http://litextension.com/