Here, The Magneto development services provider will explain to you the plugin system in Magento 2. You will learn the way of how to use Magento 2 plugins from the basics level. 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.) of the container.
What are plugins?
Magento 2 plugins provide a mechanism to effectively manage this type of "rewriting" and to minimize conflicts between extensions to avoid having to override a class to modify a simple.
As of today, plugins can only be used to rewrite public methods and are so subject to the below limitations:
- Final methods
- Non-public methods (protected or private)
- Static methods
- Constructor ( _construct)
- Virtual Types
Magento provides three types of different plugins:
Use it to change the arguments provided to a method.
Use is to change the behavior of a method.
To "rework" the output of a method.
Declaration of a plugin
Any plugins must be declared in your module's file di.xml of your module. This file can be placed, depending on its use, either within the directory, etc. Your module (plugin to use the backend and frontend) or in etc/frontend(limited use in frontend) or etc/adminhtml(limited use in backend):
The following can be specified:
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).
If you want to disable the plugin, set this parameter to true.
Execution order of the plugins
When multiple plugins rewriting the same method exist, the Magento will follow the following order of performance:
- The type of plugin beforethat has the highest priority (= the one with the least sortOrder)
- The type of plugin aroundthat has the highest priority (= the one with the least sortOrder)
- The other type of plugins beforebased on their priority (from smallest to largest sortOrder)
- The other type of plugins aroundbased on their priority (from smallest to largest sortOrder)
- The type of plugin afterthat has the lowest priority (= the one with the largest sortOrder)
- The other type of plugins afterbased on their priority (largest to smallest sortOrder)
To change the settings passed to a function, it is necessary to create a method before [methodName] in your plugin (which [methodName] corresponds to not the method you want to change, for example, SetName for a method named setName).
The method created within your plugin parameter must take the class that 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 $name will be automatically enclosed in parentheses.
As you have probably noticed, the type of plugins before has returned an array containing all the parameters of the function.
A method around [methodName] must be created within your plugin (which [methodName] corresponds to not the method you want to change, for example, Save for a method named save) to change the behavior of a function, a. The method created within your plugin parameter should take the class that includes the function you want to change and another type parameter \Closurecorresponding to the parent method.
Example of method overloading saveclass \Magento\Catalog\Model\Product:
In this example, we execute line 9 that will retrieve the original method and the return value. If it is defined, a third function, called here do Something, will be executed. Finally, we return line 13, returning the original method.
It is necessary to create a method after[methodName] in your plugin (which [methodName] corresponds to not the method you want to change, for example, GetName for a method named getName) to change the return of a function.
The method created within your plugin parameter must take the class that 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 "|". Magento developers from India have shared the definition, limitations, and uses of Magento 2 plugins for reference purposes only. You can ask for more info on the Magento 2 plugins in your comments.