Brief Summary
This video provides a comprehensive guide to using plugins in Magento 2, focusing on before
, after
, and around
plugins. It explains how to create and configure plugins to modify the behaviour of existing methods, using the example of a product key class. The video covers intercepting method parameters, altering results, and executing code before and after the original method.
- Creating and configuring plugins in Magento 2.
- Using
before
,after
, andaround
plugins to modify method behaviour. - Handling default parameter values in plugins.
Introduction to Plugins
The video introduces the concept of plugins in Magento 2, using a module plugin example with a controller, model, and view model. The basic functionality involves capturing a product ID from the URL, loading the product, and formatting a product key for display. The focus is on plugging into the getProductKey
method of a product key class to modify its behaviour.
Creating a "Before" Plugin
To create a plugin, a new class ProductKeyPlugin
is created with a beforeGetKey
method. This method accepts the subject (the original class), the product interface, and a string prefix as parameters. The beforeGetKey
plugin allows you to execute code before the original method, manipulate the parameters, and return the modified parameters. The plugin is then defined in the di.xml
file, specifying the subject and the plugin type.
Demonstrating the "Before" Plugin
The video demonstrates how the beforeGetKey
plugin can modify the prefix parameter. By intercepting the parameters, the plugin changes the prefix to include the product ID. It also explains that when a method has a default value for its parameters, the plugin should also define this parameter with a default value to avoid errors.
Creating an "After" Plugin
The video explains how to create an after
plugin using the afterGetKey
method. This method accepts the subject, the result of the original method, and the parameters. The afterGetKey
plugin allows you to modify the result of the original method. The video demonstrates how to amend the result by adding a string "item from plugin" and also how to modify the result based on the product name.
Creating an "Around" Plugin
The video details the creation of an around
plugin using the aroundGetKey
method. This method accepts the subject, a callable proceed
function, and the parameters. The aroundGetKey
plugin gives you full control over the execution of the original method. You are responsible for executing the original method by calling proceed
and can run code both before and after this execution. The video demonstrates how to modify the result by adding the product ID, SKU, and product name.
Using Arguments in "Around" Plugins
The video shows how to use arguments in around
plugins. If the parameters are not needed individually, they can be passed as a single argument using args
. This simplifies the method signature and allows for more flexible parameter handling.
Conclusion
The video concludes by summarising the use of before
, after
, and around
plugins in Magento 2. It highlights the capabilities of each plugin type and encourages viewers to like and subscribe for more content.