2

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?

3 Answers 3

2
  1. Create fields in the admin through system.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../Magento/Config/etc/system_file.xsd">
    <system>
        <tab id="zero" translate="label" sortOrder="10">
            <label>Zero - Customerreview</label>
        </tab>
        <section id="customerreview" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>Customerreview</label>
            <tab>zero</tab>
            <resource>Magento_Config::config</resource>
            <!-- <resource>VENDOR_MODULE::path_to_the_acl_resource</resource> -->
            <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>General Configuration</label>                
                <field id="review_auth" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Authorization Token</label>
                    <comment>Enter Authorization Token.</comment>
                </field>
                <field id="review_account_id" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Account Id</label>                    
                </field>
            </group>
        </section>
    </system>
</config>
  1. Get value from the admin configuration
<?php
namespace Zero\Customerreview\Block;

class Customerreview extends \Magento\Framework\View\Element\Template
{
     protected $helper;
        
     public function __construct(        
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,     
        array $data = []
     ) {        
        $this->scopeConfig = $scopeConfig;
        parent::__construct($context, $data);
    }
  
    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        
    }
    public function getCustomerReview(){   
            
        //Here you need to check system.xml file and get section_id/group_id/field_id
        $authentication = $this->scopeConfig->getValue('customerreview/general/review_auth', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
        $accountId = $this->scopeConfig->getValue('customerreview/general/review_account_id', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

        $authorization = "Authorization:".$authentication;

        

        $url = 'https://mybusiness.googleapis.com/v4/accounts/'.$accountId.'/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;
    }
}
1
  • can u pls help me with that token issue, i shared the script, thanks.
    – zus
    Apr 30, 2021 at 12:00
1

To get data from admin configurations

First of all create a directory "Helper" inside Zero\Customerreview/Helper and then create a file name Zero\Customerreview/Helper/Data.php inside Helper

<?php
namespace Zero\Customerreview\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
        );
    }
}

Now in your block or controller call helper

<?php
namespace Zero\Customerreview\Block;

class Customerreview extends \Magento\Framework\View\Element\Template
{
     protected $helper;
        
     public function __construct(
        \Zero\Customerreview\Helper\Data $helper,
        \Magento\Framework\View\Element\Template\Context $context,        
        array $data = []
     ) {
        $this->helper = $helper;
        parent::__construct($context, $data);
    }
  
    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        
    }
    public function getCustomerReview(){   
            
        //Here you need to check system.xml file and get system_id/group_id/field_id
        $abc = $this->helper->getConfig('system_id/group_id/field_id');
        //echo $abc;
        return $abc;
    }
}
2
  • May i know what is mean system_id/group_id/field_id
    – zus
    Apr 30, 2021 at 4:35
  • You can check this in your system.xml file from the module you want to get data Apr 30, 2021 at 5:04
1

@zus system_id/group_id/field_id which is getting from your module system.xml

  1. system_id means Section id from your system.xml
  2. group_id means group id which is under section tab
  3. field_id means field id which is under the group section and that thing used for getting system configuration value.

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.