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 😉

bluethinkinc_blog

bluethinkinc_blog

2024-02-06

0

Leave a Reply

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

Find More Blogs

Proxy Design Pattern and Requisition List in Magento 2

Spread the love Requisition List in Magento 2 Requisition

Google Ads Conversion Tracking Through Magento Platform:

Spread the love Google Ads Conversion Tracking Through

Object Pool Pattern in Magento2

Spread the love What is Object Pool Pattern in Magento 2 An

JENKINS

Spread the love JENKINS Introduction and Installation

Virtual Type and Type

Spread the love Virtual type is one of the most common

Customize the length of Input field

Spread the love Sometimes we need to change the limit the

How to Add Custom Tab on Admin Dashboard and Count Website Total Visitor in Magento 2?

Spread the love In this blog we’ll see How to Add Custom

How to Add Sign Out Tab in Customer Account in Magento 2?

Spread the love In this blog we’ll see How to Add

How to create custom tab in product detail page in magento 2?

Spread the love In this blog we’ll see how to add

Understanding and Implementing CORS & CSRF

Spread the love What is security? The world has become more

bluethinkinc Blogs