Magneto development services provider will explain plugin system in Magento 2. You will learn the way to use magento 2 plugins from the basics. Read and discover how professionals do it.
You will learn through this article how to use Magento 2 plugins that modify the behavior of all public functions without overloading the PHP class (block, model etc.) the container.
To avoid having to override a class to modify a simple, Magento 2 plugins provides a mechanism to effectively manage this type of "rewriting" as well as to minimize conflicts between extensions.
To date, plugins can only be used for rewriting public methods and are therefore subject to the following limitations:
Magento provides three "types" of different plugins:
Change the arguments provided to a method
Change the behavior of a method
To "rework" the output of a method
The declaration of any plugin must be done in the file di.xml of your module. This file can be placed, depending on its use, either within the directory etcyour module (plugin to use the backend and frontend) or in etc/frontend(limited use in frontend) or etc/adminhtml(limited use in backend):
The class name, or virtual interface type you want to observe
The name of the plugin (eg. catalog_product_custom_plugin)
The name of the class or type used by the virtual module (ex.Vendor\Module\Plugin\ModelNamePlugin)
The order in which the plugin will run. Very useful to effectively manage the execution order of several plugins overloading the same method (see "Execution Order" below for details)
This parameter can be set trueif you want to disable the plugin
When multiple plugins rewriting the same method exist, the following order of performance will be followed by Magento:
To change the settings passed to a function, it is necessary to create a methodbefore[methodName]in your plugin (which [methodName]corresponds to not the method you want to change, for example SetNamefor a method named setName).
The method created within your plugin parameter must take the class which includes the function you want to change and its various parameters.
Example of method overloading setNameclass \Magento\Catalog\Model\Product:
In this example, the product name passed in the argument $namewill be automatically enclosed in parentheses.
As you have probably noticed, the type of plugins beforehave returned an array containing all the parameters of the function.
To change the behavior of a function, a method around[methodName]must be created within your plugin (which [methodName]corresponds to not the method you want to change, for exampleSavefor a method named save). The method created within your plugin parameter must take the class which includes the function you want to change and a second type parameter \Closurecorresponding to the original method.
Example of method overloading saveclass \Magento\Catalog\Model\Product:
In this example, we execute line 9 will recover the original method and the return value. If it is defined, a third function, called here doSomething, will be executed. Finally, we return line 13 returning the original method.
To change the return of a function, it is necessary to create a method after[methodName]in your plugin (which [methodName]corresponds to not the method you want to change, for exampleGetNamefor a method named getName).
The method created within your plugin parameter must take the class which includes the function you want to change and a variable $resultthat will contain the result of the original method.
Example of method overloading getNameclass \Magento\Catalog\Model\Product:
In this example, the return of the original method, which is here in the name of the product, will be surrounded by "| ". The definition, limitations and uses of magento 2 plugins shared by magento developers from India for reference purpose only. You can ask for more info on the Magento 2 plugins in your comments.