What is the Basic difference between type and virtualType

Hello everyone, sometimes you are in a situation where you need to use the class, for example, the collection of the sales order grid (Magento\Sales\Model\ResourceModel\Order\Grid\Collection) (Sales_order_grid UI components)

But you need to use a different value for the parameter, for example, you want to change $resourceModel.

If you don’t know about virtualType yet, you will have to create a new class that extends Namespace\Modulename\Model\ResourceModel\Orders\Grid\Collection and overrides the value.

With the help of virtualType, you can do like this:

 <virtualType name="Namespace\Modulename\Model\ResourceModel\Orders\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
        <arguments>
            <argument name="mainTable" xsi:type="string">modulename_orders</argument>
            <argument name="resourceModel" xsi:type="string">Namespace\Modulename\Model\ResourceModel\Orders\Collection</argument>
        </arguments>
    </virtualType>


    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="modulename_orders_listing_data_source" xsi:type="string">Namespace\Modulename\Model\ResourceModel\Orders\Grid\Collection</item>
            </argument>
        </arguments>
    </type>

That’s everything you need to do to change resource model of sales order grid.

Leave a Reply

Your email address will not be published. Required fields are marked *