Magento UI Component Magical Data Providers

Published: February 27, 2019

Tags:

UI components are notoriously one of the most painful aspects of working with Magento 2.

One aspect that’s thrown me for a loop is the “magical data providers”. For example, if you look at vendor/dotdigital/dotmailer-magento2-extension/view/adminhtml/ui_component/dotdigitalgroup_order_grid.xml you’ll see the following…

<dataSource name="order_report_grid_data_source">
    <!--The data source-->
    <argument name="dataProvider" xsi:type="configurableObject">
        <argument name="class" xsi:type="string">Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider</argument>
        ...

But how could the Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider class be responsible for providing data to the order report grid?

As with most magic in Magento 2 the answer can be found in di.xml. There you’ll find the following…

<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
    <arguments>
        <argument name="collections" xsi:type="array">
            <item name="order_report_grid_data_source" xsi:type="string">Dotdigitalgroup\Email\Ui\Model\ResourceModel\Order\Collection</item>
            ...

As you can see the if ui_component uses Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider as it’s data provider the actual data provider can be declared in di.xml.

In Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory we can see how Magento consults it’s collections property to get an instance of the dataProvider class.

public function getReport($requestName)
{
    if (!isset($this->collections[$requestName])) {
        throw new \Exception(sprintf('Not registered handle %s', $requestName));
    }
    $collection = $this->objectManager->create($this->collections[$requestName]);
    ...

Max Chadwick Hi, I'm Max!

I'm a software developer who mainly works in PHP, but also dabbles in Ruby and Go. Technical topics that interest me are monitoring, security and performance.

During the day I solve challenging technical problems at Something Digital where I mainly work with the Magento platform. I also blog about tech, work on open source and hunt for bugs.

If you'd like to get in touch with me the best way is on Twitter.