Virtual Type and Type

Blog
Spread the love

Virtual type is one of the most common design patterns in magento2. Basically, it allows us to modify existing classes without affecting other classes and inject them where we need them

Type allows us to modify dependencies injected to a constructor.

Consider following chunk of code:

We basically pass an object of our Bluethink\Crud\Model\ResourceModel\Post\Grid\Collection class to the Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory constructor of the bluethink_post_listing_data_source variable.

But type isn’t just about objects. It can be any value (e.g. array) that can be passed to the constructor.

has two attributes name and type, type is an existing class and name is virtual class (It does not need to create). Virtual types also can be used to manipulate the constructor argument of an existing class.

Consider following chunk of code:

The above code is almost same as creating a PHP class Grid:

Here is another chunk of code:

In the above example, We are modifying the constructor argument mainTable and resourceModel of class Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult

Consider you want to make a coffee for a party and each guest is told to bring a cup of coffee.

First, use the Espresso class in your code, where you’ll throw the coffee cups’ contents.

Now, you would put all coffee flavors into the Cup class:

Now collect the flavor cup and then move them into the Espresso. Let’s assume you first create 2 cups; one with Amaretto & chocolate and another with Hazelnut & Almond.

Next, we have our cups ready, let’s move them inside Espresso:

Conclusion: Now the cups are now in the espresso, and you are ready to make coffee 😉

Vikas Mishra

Vikas Mishra

2024-02-06

0

Leave a Reply

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

Find More Blogs

Enhancing Productivity with Dynamics 365: The Power of Session Restore

Spread the love Enhancing Productivity with Dynamics 365:

Salesforce Admin

Spread the love In our last blog, we discussed common

Unlocking AI with Spring Boot: Real-World Use Cases and Solutions

Spread the love Artificial Intelligence (AI) has moved from

Enhancing Performance with Reactive Java: A Beginner-Friendly Guide

Spread the love In today’s fast-paced software

Integrating Azure Function with Dynamics 365

Spread the love Signup on azure portal and make sure to

How to Create a Power Apps Component Framework (PCF) with React

Spread the love If youʼve been looking to supercharge your

Customize lead qualification for a seamless experience

Spread the love When the 2024 Release Wave 2 was launched,

Create a Custom Console Command in Spryker

Spread the love In Spryker, developers can create custom

SALESFORCE

Spread the love Salesforce is a cloud-based software

CRUD Operation in Spryker Backoffice with a Custom Module

Spread the love Spryker is a highly flexible and modular

bluethinkinc Blogs