Interception In Magento 2
The aim of this article is to shed some light on interceptors in Magento 2 and give some insight into how they differ from observers. I will try to explain to the the best of my ability and understanding how they both work. I will also, somewhat briefly, provide a basic example of how you would go about integrating this.
Interception is not unique to Magento 2, it’s a programming pattern. You’re more than likely familiar with Object-oriented programming, however you may not be familiar with Aspect-oriented programming.
Aspect Oriented Programming helps increase modularization through-out an application, which you’ve hopefully noticed Magento 2 has done. By using a
pointcut, we can access a specific point in our application and perform an action, this is very similar to event/observers in Magento. If you’d like to learn more about AOP I’d highly suggest watching Daniel Sloof’s talk on Aspect Oriented Magento.
The difference between interception and the observer pattern however is that we don’t need a specific event to listen to. I’m sure many of you have been in a situation in Magento 1 where you’ve need to change some functionality but there wasn’t an event being fired, this would lead to rewrites and other code smells.
Anton Krill describes Interception as “Ability observe public method calls in application”, which sums it up nicely. source here.
Interceptors can provide functionality:
- Before a method
- After a method
- Around a method
Interception Integration In Magento
Okay, so first of all interceptors are referred to as plug-ins in Magento 2. As Magento 2 is a configuration based system, like always we define our plug-in within our XML config.
You will declare you plugin in
di.xml, which makes sense.
- Type Name: The class you want to intercept
- Plugin Name: Your unique name
- Plugin Type: The class name of your interceptor class or plug-in
- Sort Order: Sets the priority should two modules conflict and be overriding the same method
- Disabled: Is the plugin active
Okay now we have done this we can create our plugin class:
The method I would like to intercept is
Magento\Catalog\Model\Product::getName() so I can return my own product name for whatever reason; to do this is very simple, check out the class:
If you’d like to read more on integration I would recommend just checking the Dev Docs Magento plug-ins. They will be maintained to a much higher degree than this article will, also the docs cover this in more depth.
What is going on behind the scenes?
Factory classes, Magento 2 auto-generates
Interceptor classes on the fly within the
The Interceptor class will use the
Example of a auto-generated class:
I deleted most of the functions in this class because it was huge, you only need to see one to understand what is going on. So Magento will instantiate this class which sits on top of the original class, it then auto-generates every single
public function which belongs to the class (which I removed).
$pluginInfo variable is set, it will proceed to the
___callPlugins within the trait:
Now, depending on the type mentioned earlier (before, after, around) it will fire your method with any appropriate arguments, this gives us the option to alter or log data at any given point.
It’s my opinion integrating this pattern provides huge flexibility and helps us developers stick to the
O in our SOLID principle; Open-Closed: “software entities … should be open for extension, but closed for modification.” - Wikipedia.