Question

In Magento 2.1, there are 25 total UI Component listing/grid data providers configured and in use. Their data provider classes and ui_component files are listed below

Magento\Bundle\Ui\DataProvider\Product\BundleDataProvider                     bundle_product_listing.xmlMagento\Catalog\Ui\DataProvider\Product\Attributes\Listing                    product_attributes_grid.xml
Magento\Catalog\Ui\DataProvider\Product\ProductCustomOptionsDataProvider      product_custom_options_listing.xml
Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider                   configurable_associated_product_listing.xml
Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider                   product_listing.xml
Magento\Catalog\Ui\DataProvider\Product\Related\CrossSellDataProvider         crosssell_product_listing.xml
Magento\Catalog\Ui\DataProvider\Product\Related\RelatedDataProvider           related_product_listing.xml
Magento\Catalog\Ui\DataProvider\Product\Related\UpSellDataProvider            upsell_product_listing.xml
Magento\Cms\Ui\Component\DataProvider                                         cms_block_listing.xml
Magento\Cms\Ui\Component\DataProvider                                         cms_page_listing.xml
Magento\ConfigurableProduct\Ui\DataProvider\Attributes                        product_attributes_listing.xml
Magento\Customer\Ui\Component\DataProvider                                    customer_listing.xml
Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider          customer_online_grid.xml
Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider          sales_order_creditmemo_grid.xml
Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider          sales_order_grid.xml
Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider          sales_order_invoice_grid.xml
Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider          sales_order_shipment_grid.xml
Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider          sales_order_view_creditmemo_grid.xml
Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider          sales_order_view_invoice_grid.xml
Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider          sales_order_view_shipment_grid.xml
Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider          search_synonyms_grid.xml
BraintreeTransactionsDataProvider (virtual type)                              braintree_report.xml
    Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider    
Magento\GroupedProduct\Ui\DataProvider\Product\GroupedProductDataProvider     grouped_product_listing.xml
Magento\Review\Ui\DataProvider\Product\ReviewDataProvider                     review_listing.xml
Magento\Theme\Ui\Component\Design\Config\DataProvider                         design_config_listing.xml

Based on this information, there appear to be two base classes end user programmers can use to base their grid components on

  • Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider
  • Magento\Ui\DataProvider\AbstractDataProvider

The Magento\Ui\DataProvider\AbstractDataProvider class seems to simpler of the two, and (seems to?) only requires the configuration of a Magento resource model. The customer grid Magento\Customer\Ui\Component\DataProvider module is based on this class, and appears to have all sorting, filtering, etc. functionality needed for a grid listing.

Is there a reason the Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider exists -- or is it just older/newer code that take a different approach to creating a data provider? In other words, does using the Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider bring any extra features to the table, or enable other parts of the system to do things with the grid? Looking at the source code, the Magento\Framework\App\RequestInterface seems intriguing -- as it implies you might be reporting functionality "for free" with these grids. However, without an extensive code safari, I'm not sure if that's true or not, and I'm hoping someone has a clear explanation of why you'd use one class over the other.

Was it helpful?

Solution

To me, that main difference is that the Magento/Framework/View/Element/UiComponent/DataProvider/DataProvider uses the Search API.

The following classes are used in this class :

  • Magento\Framework\Api\FilterBuilder
  • Magento\Framework\Api\Search\ReportingInterface
  • Magento\Framework\Api\Search\SearchCriteria
  • Magento\Framework\Api\Search\SearchCriteriaBuilder
  • Magento\Framework\Api\Search\SearchResultInterface

Which are used for filtering / ordering / paging:

public function addFilter(\Magento\Framework\Api\Filter $filter)
{
    $this->searchCriteriaBuilder->addFilter($filter);
}

public function addOrder($field, $direction)
{
    $this->searchCriteriaBuilder->addSortOrder($field, $direction);
}

public function setLimit($offset, $size)
{
    $this->searchCriteriaBuilder->setPageSize($size);
    $this->searchCriteriaBuilder->setCurrentPage($offset);
}

And also obviously for the search:

public function getData()
{
    return $this->searchResultToOutput($this->getSearchResult());
}

protected function searchResultToOutput(SearchResultInterface $searchResult)
{
    $arrItems = [];

    $arrItems['items'] = [];
    foreach ($searchResult->getItems() as $item) {
        $itemData = [];
        foreach ($item->getCustomAttributes() as $attribute) {
            $itemData[$attribute->getAttributeCode()] = $attribute->getValue();
        }
        $arrItems['items'][] = $itemData;
    }

    $arrItems['totalRecords'] = $searchResult->getTotalCount();

    return $arrItems;
}

public function getSearchResult()
{
    return $this->reporting->search($this->getSearchCriteria());
}

What's interesting if that the Magento/Ui/DataProvider/AbstractDataProvider mentions the Search API but don't use it at all :

public function getSearchCriteria()
{
    //TODO: Technical dept, should be implemented as part of SearchAPI support for Catalog Grids
    return null;
}

public function getSearchResult()
{
    //TODO: Technical dept, should be implemented as part of SearchAPI support for Catalog Grids
    return $this->getCollection();
}

Now if you check the history of those files in GitHub here's what you get:

As you can see most of the commits for those two files are linked to the following internal ticket: MAGETWO-39905: UI components compatibility with Search API

Even if it has been done for the Magento/Framework file it has never been done for the Magento/Ui file.

Apart from that I don't see any difference between those files. One is working directly on the collection, the other one is using the Search API to generate the results.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top