0

I have created a custom module that's purpose is to manage Vendors. I have created a Collection, CollectionFactory, DataProvider, di.xml, vendors_vendor_listing.xml and so on and so forth.

I am able to see that the records are being found and am able to see the items in the system logs from the DataProvider, however the grid never loads up the items in the ui component vendors_vendor_listing. The spinner just persists and the items never load.

I do not see any ajax calls in the network tab with the params namespace=vendors_vendor_listing_data_source as I would expect their to be.

Here is my folder structure

app
└── code
    └── TWW
        └── Vendors
            ├── Controller
            │   └── Adminhtml
            │       └── Index
            │           ├── Delete.php
            │           ├── Index.php
            │           └── Save.php
            ├── etc
            │   ├── adminhtml
            │   │   ├── acl.xml
            │   │   ├── menu.xml
            │   │   └── routes.xml
            │   ├── di.xml
            │   └── module.xml
            ├── Model
            │   ├── ResourceModel
            │   │   └── Vendor
            │   │       ├── Collection.php
            │   │       └── Vendor.php
            │   ├── Vendor.php
            │   └── VendorFactory.php
            ├── Setup
            │   └── InstallSchema.php
            ├── Ui
            │   └── Component
            │       └── DataProvider.php
            ├── view
            │   └── adminhtml
            │       ├── layout
            │       │   ├── vendors_index_index.xml
            │       │   └── vendor_form.xml
            │       ├── templates
            │       │   └── js.phtml
            │       └── ui_component
            │           └── vendors_vendor_listing.xml
            └── registration.php

My DataProvider:

<?php
namespace TWW\Vendors\Ui\Component;

use Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider as UiDataProvider;
use Magento\Framework\Api\Search\ReportingInterface;
use Magento\Framework\Api\Search\SearchCriteriaBuilder;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Api\FilterBuilder;
use Psr\Log\LoggerInterface;
use TWW\Vendors\Model\ResourceModel\Vendor\CollectionFactory as VendorCollectionFactory;

class DataProvider extends UiDataProvider
{
    protected $collection;
    protected $logger;

    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        VendorCollectionFactory $collectionFactory,
        ReportingInterface $reporting,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        RequestInterface $request,
        FilterBuilder $filterBuilder,
        LoggerInterface $logger,
        array $meta = [],
        array $data = []
    ) {
        $this->collection = $collectionFactory->create();
        $this->logger = $logger;
        parent::__construct(
            $name,
            $primaryFieldName,
            $requestFieldName,
            $reporting,
            $searchCriteriaBuilder,
            $request,
            $filterBuilder,
            $meta,
            $data
        );
    }

    protected function prepareUpdateUrl()
    {
        if (!isset($this->data['config']['filter_url_params'])) {
            return;
        }
        foreach ($this->data['config']['filter_url_params'] as $paramName => $paramValue) {
            if ('*' == $paramValue) {
                $paramValue = $this->request->getParam($paramName);
            }
            if ($paramValue) {
                $this->data['config']['update_url'] = sprintf(
                    '%s%s/%s/',
                    $this->data['config']['update_url'],
                    $paramName,
                    $paramValue
                );
                $this->addFilter($this->filterBuilder->setField($paramName)->setValue($paramValue)->setConditionType('eq')->create());
            }
        }
    }

    public function getData()
    {
        $this->prepareUpdateUrl();
        $searchCriteria = $this->getSearchCriteria();
        
        $collection = $this->collection;
        $collection->addOrder($this->getPrimaryFieldName(), 'ASC');

        $this->logger->info('Collection Items', ['items' => $collection->getItems()]);

        foreach ($searchCriteria->getFilterGroups() as $filterGroup) {
            foreach ($filterGroup->getFilters() as $filter) {
                $condition = $filter->getConditionType() ?: 'eq';
                $collection->addFieldToFilter($filter->getField(), [$condition => $filter->getValue()]);
            }
        }

        $collection->load();
        $items = [];
        foreach ($collection as $item) {
            $items[] = $item->getData();
        }

        $searchResult = [
            'totalRecords' => $collection->getSize(),
            'items' => $items,
        ];

        $this->logger->info('DataProvider Data', ['data' => $searchResult]);
        return $searchResult;
    }
}

My vendors_vendor_listing.xml ui component:

<?xml version="1.0"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <dataSource name="vendors_vendor_listing_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">TWW\Vendors\Ui\Component\DataProvider</argument>
            <argument name="name" xsi:type="string">vendors_vendor_listing_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">entity_id</argument>
            <argument name="requestFieldName" xsi:type="string">entity_id</argument>
        </argument>
        <argument name="data" xsi:type="array">
            <item name="js_config" xsi:type="array">
                <item name="provider" xsi:type="string">vendors_vendor_listing.vendors_vendor_listing_data_source</item>
                <item name="deps" xsi:type="string">vendors_vendor_listing.vendors_vendor_listing_data_source</item>
            </item>
            <item name="config" xsi:type="array">
                <item name="update_url" xsi:type="url" path="mui/index/render"/>
                <item name="filter_url_params" xsi:type="array">
                    <item name="entity_id" xsi:type="string">*</item>
                </item>
            </item>
        </argument>
    </dataSource>
    <columns name="vendors_columns">
        <column name="entity_id">
            <settings>
                <label translate="true">ID</label>
            </settings>
        </column>
        <column name="name">
            <settings>
                <label translate="true">Name</label>
            </settings>
        </column>
        <column name="email">
            <settings>
                <label translate="true">Email</label>
            </settings>
        </column>
        <column name="created_at">
            <settings>
                <label translate="true">Created At</label>
            </settings>
        </column>
    </columns>
    <actionsColumn name="actions">
        <settings>
            <indexField>entity_id</indexField>
        </settings>
    </actionsColumn>
</listing>

and my 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="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="vendors_vendor_listing_data_source" xsi:type="string">TWW\Vendors\Model\ResourceModel\Vendor\Collection</item>
            </argument>
        </arguments>
    </type>

    <type name="TWW\Vendors\Ui\Component\DataProvider">
        <arguments>
            <argument name="collectionFactory" xsi:type="object">TWW\Vendors\Model\ResourceModel\Vendor\CollectionFactory</argument>
            <argument name="logger" xsi:type="object">Psr\Log\LoggerInterface</argument>
        </arguments>
    </type>
</config>

Again, my custom table is set up correctly and has entries. I am correctly loading in the right ui component from vendors_index_index.xml. What do i need to do to see the correct ajax request in the network tab (and what should that be?) and ultimately, how do i get my data to show in the grid?

0

Browse other questions tagged or ask your own question.