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 loves dabbling in other languages like Go and Ruby. Technical topics that interest me are monitoring, security and performance. I'm also a stickler for good documentation and clear technical writing.

During the day I lead a team of developers and solve challenging technical problems at Rightpoint where I mainly work with the Magento platform. I've also spoken at a number of events.

In my spare time I blog about tech, work on open source and participate in bug bounty programs.

If you'd like to get in contact, you can find me on Twitter and LinkedIn.