Plugins

 

 

Let's talk about plugins."Plugins" extend a method and do not modify the class itself as class preferences do, but rather intercept the method being called before, after, or around its execution.Let's add some additional method here.Something like "set Some Value" and "get some value".Let's clear the cache and change the index controller a bit.Let's check the result.Great!Now we have the result of this method.Let's try to add a plugin for this particular method.To do this we need to add the following configuration to the di.xml file.First of all, we need to specify the name of the class that will be overridden.In our case it will be SomeClassName.We need to specify the name of the plugin, it should be "some unique id"we also need to specify the plugin class.As you remember, Magento supports the PSR-4 standard, so the class can be located in any folder.But Magento recommends using the Plugin folder for plugins, so let's use something like Plugin Test.phpand let's add some PHP code here.Let's specify this class name for our new plugin.We can specify sortOrder - it can be really useful when we have several plugins for the same method.We can also specify "disabled" as true or false.This can be very useful if we need to disable some plugin.So, plugins are called before, after, or around overridden methods.Let's create the "before" plugin first."Before" plugin methods are the first methods that run in an observable method, and these methods must have the same name as the observable method, and the prefix label must be specified as "before".In our case, we are going to overwrite setSomeValue method.We should use the function name "beforeSetSomeValue"The original class name must also be specified as the subject.Let's add this here.And we must specify all the arguments of the original method. To apply before executing the method, to change the arguments of the observed method.You can return the modified argument.You can change as many arguments as you need.But if the return is invalid, it means that the argument to the observable method should not be changed.In our case we can modify the value Let's add some additional information to our plugin.We can return as many arguments as needed for our original method.Let's clear the cache.And let's check it.Works fine! great!In the same way we can add afterSetSomeValue and aroundSetSomeValue method.Let's do this.We have to specify the original class name as the subject. But instead of arguments, we have to specify the result variable.These methods start executing immediately after the observer method completes, and these methods must have the same name as the observed method, with the "after" prefix label."After" methods are responsible for modifying the results of observed methods and must have a return value.Let's add some additional information to the result of the getSomeValue method.Let's clear the cache.And let's check it."around" plugins! "around" plugins allow code to run before and after the observed method.So you can override the method.These methods must have the same names as the observable methods and the prefix must be "around"Let's create "around" plugin for the setSomeValue method.We also need to specify the original class name.we need to add our callable proceed variable and the arguments of the initial class.So for setSomeValue method we can change the arguments but not the result.Let's add some additional information to this argument.Here we can add additional logic before the original method call.Here we will have the result of the original method, so we need to specify all the arguments here.Finally, we can add some logic after the original method call and return the result of that method.Let's add the same plugin, but for the getSomeValue method.In this case, we can change the result of this method.Let's clear the cache.And check.As you can see, our "around" plugins have been applied, but to simply check the order of the plugins, let's add a little extra information to just one setSomeValue method and check the result.Now we have all possible plugins for one method with some additional information.Let's clear the cache and check the result.Here you can see the actual sort order of the plugins.The first plugin will be "before" and the last one will be "after".The sort option allows you to arrange plugins that track the same method. Plugins are applied one after another.Additionally, we can disable some plugins with disabled=true configuration, this can be very useful if you need to change the behavior of an external plugin.But how do plugins work? We can find the automatically generated interceptor class in the /generated folder.Actually, in development mode we can clear the /generated folder and all classes will be generated automatically on demand, but in production mode we will have to run setup:di:compile command which will also generate the required classes for all modules.Magento uses this class to call all plugins.And as you can see, it extends the original one. So you can't use plugins for final methods, final classes, any class that contains at least one final public method, static methods, real constructor, and most importantly, you can't use plugins for non-public methods.If we need to add additional logic that will not affect the flow or change the data, such as logging, it is better to use events.
You have completed 0% of the lesson
0%