Maximizing Magento 2 Customization with Plugins (Interceptor)

Posted on: 05 Nov 2019 by Admin

Magento 2 plugins come with 2 different versions, 1X and 2X.

Here we have explained about customize a functionality using Interceptor in Magento 2 Plugins:
As the number increased the features had also got a mark in 2X with a lot more improvements

In Version 1X, customization of different classes was challenging, whereas 2x managed to solve it.
There the overwriting of a core class in your modules was not us to the mark.

However the issue happens when there are at least two modules overwriting a similar core class,
2x Helps Magento Web Development Services with a wide range of Features.

How Magento 2 features fix this core class issue?

Magento 2 introduced a new concept called Plugins. A plugin or interceptor is a class that modifies the behavior of public class functions by intercepting a function call and running code before, after, or around that function call. Plugins use the design pattern called Interception.
Let’s see how Magento 2 plugins (interceptor) works

Restrictions in using Magento 2 Plugins are:

Final methods
Final classes
Non-public methods
Static methods
__construct
Virtual types
Objects that are instantiated before Magento\Framework\Interception is bootstrapped
Objects that are not instantiated by the ObjectManager (e.g. by using new keyword directly to create class object ).

Declaring a new plugin in Magento 2

You can create a new plugin for a class object using di.xml of your corresponding module at app/code/{namespace}/{module}/etc/di.xml.

Required options for a plugin

Type name: Name of a class or interface which the plugin observes.
Plugin name: An arbitrary plugin name that identifies a plugin. Also used to merge the configurations for the plugin.
Plugin type: The name of a plugin’s class or its virtual type. Use the following naming convention when you specify this element: \Vendor\Module\Plugin\Plugin.

Optional options for a plugin

SortOrder: The order in which plugins that call the same method are run.
Plugin disabled: To disable a plugin, set this element to true. The default value is false. Use this property to disable a core or third-party plugins in your di.xml file.

Types of plugins

In Magento 2, we have three types of plugins.
Before listener Plugin
After listener Plugin
Around listener Plugin
Before listener Plugin
You can use before plugin for changing the arguments of an original method or to add some behavior before an original method is called.
The name of this method will be the same as the original method with “before” as prefix and the first letter of an original method to capital.
Below is an example for before method to add additional behavior before the original method.

Step 1: Create di.xml at Vendor/Module/etc/di.xml

Step 2: Create CustomFilter.php as plugin class at Vendor\Module\Plugin\CustomFilter.php

Note: We can use Magento 2 plugin constructor (dependency injection for the plugin) if our class requires a dependency on other class methods or variables
Plugins Execution Order
We can prioritize plugins using sortOrder property when several plugins are observing the same method.
If we have two plugins name Plugin A, Plugin B and Plugin C with corresponding sort order 10, 20 and 30 observing the same method with the following properties:.

Plugin A Plugin B Plugin C
sortOrder 10 20 30
before beforeLoad() beforeLoad() beforeLoad()
around aroundLoad() aroundLoad()
after afterLoad() afterLoad() afterLoad()

The execution flow will be:
PluginA::beforeLoad()
PluginB::beforeLoad()
PluginB::aroundLoad()(until callable is called)
PluginC::beforeLoad()
PluginC::aroundLoad() (until callable is called)
Action::load()
PluginC::aroundLoad() (after callable is called)
PluginC::afterLoad()
PluginB::aroundLoad() (after callable is called)
PluginB::afterLoad()
PluginA::afterLoad()

Conclusion

By using Magento 2 plugins we can hooks our logic into the available classes without overwriting them.