1

I am trying to make a tabular/grid view in the Admin area for some data that comes from an external API. For that I implemented a data provider. In here I need to create an instance for the external API client with the config stored in the store settings.

However, I am not sure how to inject or retrieve these settings in the data provider:

class ListingDataProvider extends \Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider
{
...
 public function __construct(
         $name,
         $primaryFieldName,
         $requestFieldName,
         ReportingInterface $reporting,
         SearchCriteriaBuilder $searchCriteriaBuilder,
         RequestInterface $request,
         FilterBuilder $filterBuilder,
         array $meta = [],
         array $data = [],
          ScopeConfigInterface $scopeConfig <---- Is this possible?
     ) {
         $this->request = $request;
         $this->filterBuilder = $filterBuilder;
         $this->name = $name;
         $this->primaryFieldName = $primaryFieldName;
         $this->requestFieldName = $requestFieldName;
         $this->reporting = $reporting;
         $this->searchCriteriaBuilder = $searchCriteriaBuilder;
         $this->meta = $meta;
         $this->data = $data;
         $this->prepareUpdateUrl();
}
...
}

This is my (probably wrong) di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
 <type name="Some\Module\Ui\DataProvider\Administration\ListingDataProvider">
   <plugin name="some_module_attributes" type="Some\Module\Plugin\AddAttributesToUiDataProvider"/>
 </type>
 <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
  <arguments>
   <argument name="collections" xsi:type="array">
     <item name="some_module__administration_listing_data_source" xsi:type="string">SomeModuleAdministrationCollection</item>
   </argument>
  </arguments>
 </type>
  // Not quite sure how should this be replaced
 <virtualType name="SomeModuleAdministrationCollection" type="Some\Module\Ui\DataProvider\Administration\Listing\Collection">
   <arguments>
     <argument name="mainTable" xsi:type="string">catalog_category_entity</argument>
     <argument name="resourceModel" xsi:type="string">Some\Module\Model\ResourceModel\Administration</argument>
   </arguments>
 </virtualType>
</config>

I am sure the above is wrong, as i do not have a mainTable. But, is it possible to inject the config? Or, if I inject the config paths, how would I retrieve them in the provider class itself?

Thanks!

1 Answer 1

0

You can inject ScopeConfig into DataProvider class but as of Magento 2.4.4 (use PHP 8.1) require parameters have to declare before the optional parameters.

Assume your DataProvider has the code:

class ListingDataProvider extends \Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider
{
    ...
    public function __construct(
         $name,
         $primaryFieldName,
         $requestFieldName,
         ReportingInterface $reporting,
         SearchCriteriaBuilder $searchCriteriaBuilder,
         RequestInterface $request,
         FilterBuilder $filterBuilder,
         array $meta = [],
         array $data = []
    ) {
         $this->request = $request;
         $this->filterBuilder = $filterBuilder;
         $this->name = $name;
         $this->primaryFieldName = $primaryFieldName;
         $this->requestFieldName = $requestFieldName;
         $this->reporting = $reporting;
         $this->searchCriteriaBuilder = $searchCriteriaBuilder;
         $this->meta = $meta;
         $this->data = $data;
         $this->prepareUpdateUrl();
    }
    ...
}

You should inject ScopeConfig like this:

use Magento\Framework\App\Config\ScopeConfigInterface
use Magento\Store\Model\ScopeInterface;

class ListingDataProvider extends \Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider
{
    ...
    public function __construct(
         $name,
         $primaryFieldName,
         $requestFieldName,
         ReportingInterface $reporting,
         SearchCriteriaBuilder $searchCriteriaBuilder,
         RequestInterface $request,
         FilterBuilder $filterBuilder,
         ScopeConfigInterface $scopeConfig,
         array $meta = [],
         array $data = []
    ) {
         $this->request = $request;
         $this->filterBuilder = $filterBuilder;
         $this->name = $name;
         $this->primaryFieldName = $primaryFieldName;
         $this->requestFieldName = $requestFieldName;
         $this->reporting = $reporting;
         $this->searchCriteriaBuilder = $searchCriteriaBuilder;
         $this->meta = $meta;
         $this->data = $data;
         $this->prepareUpdateUrl();
         $this->scopeConfig = $scopeConfig;
    }
    ...
}

Then you can get store config by:

$this->scopeConfig->getValue('sectionId/groupId/fieldId', ScopeInterface::SCOPE_STORE);

For creating a Grid using UI component without a database table, take look at this document: https://developer.adobe.com/commerce/frontend-core/ui-components/components/listing-grid/#examples. And you can take a look at this post for reference too: Magento 2 UI grid non db dataprovider and pagination issue

Feel free to let me know if you still need help after that.

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.