Virtual Types

 

 

As you probably saw, we can't change the code in the vendor folder, because all the modules installed via composer, including Magento core modules, can be updated automatically via composer.We can work only in app/code or app/design folders, but not in the vendor folder, all the rest files should be updated only via composer.But the question is, how can you change the functionality of Magento core modules located in the vendor folder without changing the files in that folder?Magento provides only three possible ways to change the functionality of any other classes. These are "events", "plugins" and "preferences"Let's focus on preferences first. All classes should be defined by their interfaces in di.xml file. Interfaces should be used wherever possible, and the mapping in the di.xml file will indicate which class should be used.But what if we need to override some class that defined in some module located in the vendor folder? As you remember, we can only change the code in the app/code directory, not in the vendor folder.For example, we have some controller that uses some interface, and we need to change the implementation of this interface.First of all, let's check the current implementation.We have this controller and this execute method that will show us some allowed method.Let's check it.As we can see, we have some value for someClassName.Let's say we need to change the behavior of this someMethod.To do this, we will have to create another class that extends this one.Let's use our another test modeladd some php code here and overwrite our original method.and return another value here.And now we have to override di dot xml file.Let's get an example from this module and let's copy it to our new TestModule.OK, so now we have to specify another implementation here.Let's check the result.But even if we clear the generated folder and clear the cache.We will see the same result.So, the new di.xml configuration does not work. First, let's check the order of modules in the E T C config.xml fileAs you can see, the di.xml file from TestVendorName will be loaded before VendorName.So our di.xml file from TestVendorName will be overwritten by di.xml file from VendorName.As a result, we will see someClassName result instead of anotherClassName result.To fix this, we need to change the order of modules in the module.xml file. Let's do that.Every time we add new module or change something in module.xml file, we have to run setup upgrade command.As you can see we have VendorName_ModuleName and only then TestVendorName_TestModuleNameWe can also check the module or load order using console command.Let's do this.Same here: VendorName_ModuleName and only then TestVendorName_TestModuleName.Let's check the result.Now it works fine.But what if the change shouldn't be global, but rather specific to a method? Instead of redefining the entire class, as in this case, we can customize the arguments the class receives.Let's change the arguments for our controller class and change the implementation only for this specific controller class.To do this we need to specify the type.This should be the name of our class that will be overridden.In this case, it will be the controller class.We're going to change their arguments that this class will receive. So we can change some class argument or context argument.We can see them in the constructor of this class.Let's change the SomeClass argument.We also need to specify the type of this argument, in our case it will be an object.Let's change it to a different class name.Let's remove our preference.and let's clear, the cache.Now we can check it.But if we remove this, we will see the original result of the class name.As you can see, we can specify arguments only for a specific class.If another model tries to use this class interface, it will get someClassName, but for index action according to this configuration we will get anotherClassName.And finally, virtual types. Virtual types are a way to inject various dependencies into existing PHP classes without affecting other classes or creating a new class.What does it mean?Let's add an extra variable to our constructor here.And let's add all the necessary fieldsNow we can use this variable for the result.Let's clear the cache.And let's check it.As you can see, we have "some string" from anotherClass.But what if we want the same class but with different arguments in the constructor?The only difference between this class and the new one will be the list of constructor variables.In this case we can use Magento virtual types.For this we can create a virtual class. First of all, we need to specify the name of the virtual class.We also need to specify the class that will be overridden in our case, this will be AnotherClassNameAnd now we can specify new arguments to this class, in our case it can be some string valueLet's specify the type of this argument.And let's set a new value.So now we have another one class with the name - virtualClassName, which differs from anotherClassName only by different argument values.Now we can use this new virtualClassName in di.xml fileAs a result, the Index Action Class will receive a virtualClassName based on the another class name, but with a new value for the arguments for that class.Let's clear the cache and check the result.Works fine!Virtual types are useful when you only need to change the arguments of a constructor without creating additional files.Let's imagine you have a huge Magento base class with a lot of constructor arguments. But the only thing you need to change is just one constructor argument value.In this case, you can use a virtual type.You can create a new virtual type that is based on this class. Then you can change the argument values, and then you can use this virtual class in the di.xml file.There are several ways to override classes and methods, and the choice of which one to use will depend on the situation.Note that using class preference will work in most situations, but it can lead to numerous conflicts when different modules try to override the same classes and methods.But remember that according to "Magento best practices" it is better to use event observers and plugins whenever possible.
You have completed 0% of the lesson
0%