How to create and use plugins in Magento 2

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, and around 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.

Summarize any YouTube video in seconds

Get summaries like this one for any video, in your language, with the free Summ app.

Download on the Apple Store
Get it on Google Play

Transcript

Full transcript of “How to create and use plugins in Magento 2”. Click a timestamp to open the video at that moment. Need another video? Use the YouTube transcript generator.

0:00 hello guys in this video we will look at the plugins and see how they work we already created the module but we just need to work out the plugin so i've made the this page and in the code we created the module plugin example with the controller and model view model views and etc so the basic functionality here is we have a controller and then we have also a view module view module will just capture the id from the url and which is the product id and then we'll load the product and then pass it into the product key in order to format the product key in order to display it here as you can see and this is how it works on

0:45 so this is the product key class we will plug in into these this method now we need to remove this default value and then we will come back later to it however we will so if you go here get key and then we will have product here and we can just work out that's fine now first of all in order to create a plugin i will just create plugin and will be product key plugin last name will be product key plugin and the first we are trying to do is we will have the b4 now we will say public function before get key now we pass in the subject which is this class so we just copy it and

1:31 paste it here and then we call it subject so this is the first parameter we'll accept in the plugin and what else we will have we will have those parameters so i will just copy them and place them here so we have the product interface and then the string prefix in order to plug in the get key the before get key will work on you do some functionality here do something and then return the parameters the parameters will be product and prefix so this will work just fine now since we have the class we created we will define this in the dixml file so we will have di dot xml and then in the

2:16 dixml file we will have the dii and then the type type will be the product key choose the subject this is the subject and then we will have plugin plugin we will just say product key plugin and then the type will be the plugin itself so here we go now we created the plugin and it should work just fine if we just go and clear cache cache config and if we just reload the page we got an error and got exception the xml file is and name five this is it clearly config now if we refresh again so it just works fine there's nothing yet so if we go to the to the plugin now we can do the following product we don't want to play around with because it will be loaded from the

3:03 database but what we can do here the product will have the prefix will have prefix and then equals product get id and then colon and then prefix and this will show us prefix instead of product will be product colon or we just call it dash then product id and then prefix so if we refresh now we will have one product because we pass in the id01 if we pass the id of 2 we get the e2 product the 2 comes from the product not from the parameter because if the parameter doesn't exist like if we say this number so we get an error and now we have this and all these numbers coming from the product so here it is and this is how you do the plugin for this one

3:49 is fine you can manipulate the parameters and then return them so instead of having some parameters being passed in you can intercept them and then make changes to the parameters and then return them or you can do extra functionality it doesn't have to be the parameters it could be anything so just before get key just do any other function so before getting the key you will do extra stuff and this is where the before plugin comes into place now so what we need to do here we need to know about a cache here so in the method product key if i have a default parameter and i will call a product or just say item and then if i run it in here we'll throw an error this is because

4:34 when you have a default value for the parameter you need also to have it in the plugin so you will have item for example and this should fix it oh declaration training now it will be fixed if we just remove generated code training we'll need to remove the generated code and this will remove it so every time you have a method with default value for its parameters when you have a plug-in for it just try to have the the default value as well in the plug-in not just the plug-in don't make it like require because it's an optional and it's with the default value then you will have it as a default value here now this is done i think uh you would know about the before

5:20 method now the next we will need to do is the after method so let's go ahead and comment this and what we need to do is upload function after get key again we will need here the subject we'll paste the object in and i think we already imported this so we don't need it now what we need to do is the result comes next and then will be the parameters so these are the parameters i don't know whether you would need them or not because these are optional now you can let's go ahead without parameters now you have the result if you have the result and then you would say return result and then make changes to the result and just say

6:06 item from plugin and now this is after if you so this is the result and it's added the item from plugin because we are amending the result after get key which works on the result however you can also pass in the parameters if you need to so probably you want to make some calculation based on the parameters and then you return the right result for it so for example result and then you would say product get name and you would place some space between them and if you refresh you will get this as a modified result now we have this the after get key or the after plugin the next step is to create the round so the round is will be

6:52 employed function around get key so what do we have for the around first of all we have the subject here and then we have callable proceed and then we will have the parameters here are the parameters now you can see there is no result here because we don't have the result yet in the around plugin you are responsible to execute the method that is you are targeting for the plugin so the get key here is not actually triggered or executed you have to execute it yourself to execute it you just run return proceed and then you put product and prefix so this will work just fine we'll do nothing just return the stuff but we can have this result equals proceed

7:37 now i will make modification to the result product get name this is the result and this will be here and the prefix will be prefix product get id and if we run it we don't have no return so prefix what it's saying is return value of okay because it returns we will also return result as we missing the return value now we just refresh we have the product id too and then the sku and then the product name when do we need the round method the around method or the around plugin we will use it if we want to run some functionality execute code before the get key and after get key so the proceed will be in the middle and

8:23 then we will wrap it in code so we will have code before and a code after and then return the result so you always run proceed in the middle of functional two functionalities you would you would run so this is the around plugin now we have this i think we will also just demonstrate this you can have this result and then from the result you can also probably try item give it item and then return result and this should work just fine so you have the item now and because you don't use the parameters the parameters doesn't need to be like this you can also have them as arcs compact or whatever they called and then you pass in

9:09 the arcs here so these are the parameters and it will work just fine okay argument one pass you will have it like so and that's it you should you should be able to run it as arguments because you don't need them so you don't need to put as many arguments as you want so you just make them only one argument and you can proceed with that and run the code with this one so this is it this is the before and after and around plugins i hope this is useful please like and subscribe if you like it and i see you next time

Share

More video summaries