Magento 2 plugin interceptor system:

A class which will intercept and modify the behaviour of a public method is referred to as plugins.

  • A plugin(interceptor), is a class that modifies the behaviour of public class method by intercepting a method call
  • Intercepting a method call can be done by running code before, after, and around that method call
  • This allows you to substitute or extend the behaviour of original, public methods for any class or interface.

In other way plugin does:

  • Modify the return value of any function call
  • Modify the arguments of any function call
  • Modifying a method while other modules are doing the same thing to the same function

The rewrite concept of magento 1 still exist in magento 2 in the form of class preferences and argument replacement, but Magento 2 plugin Interceptors system is introduced to solve rewrite issues which was not there in magento 1. Most common issue of magento1 rewrite was, each class is only “rewritable” by a single module. Once one module claims a method, that means other modules are not able do anything. This creates conflicts between modules that need to be fixed manually if someone have deep knowledge of both Magento and solution of conflicting rewrites else his bad luck.

But plugins of magento2 has some limitation:

Plugins(interceptor) cannot be used with any of the following case:

  • __construct
  • Static methods
  • Final methods
  • Non-public methods
  • Final classes
  • Virtual types
  • Objects which are instantiated before Magento\Framework\Interception is bootstrapped
  • Any class which contains at least one final public method

Plugin declaration:

di.xml is responsible for declaring plugin class object of your module.

Attributes of above declaration:

Mandatory Attributes:

  • type name: The class or interface to which your module wants interfere
  • plugin name: An unique name that identifies your plugin name. It also used to merge the configurations for the plugin
  • plugin type: Name of plugin’s class or its virtual type. e.g: \Vendor\Module\Plugin\<ModelName>\Plugin

Optional Attributes:

  • plugin sortOrder: on the order that call same method will run
  • plugin disable: for disabling a plugin either your or third party plugin or core

Define your plugin:

Defining your plugin can be done in different situation( before, after and around). First argument of all these is an object that provides access to all public methods that your plugin observing.

Before:

This methods runs before the observed method. This methods modifies the arguments of observed method. If multiple arguments then all arguments returns in an array. and “null” identifies that the argument should not be changed.This methods name will be same as observed method with prefix “before”. e.g: beforeObservedMethod.

Through below example I am going to modify parameter value.

After:

This methods runs after the observed method. This methods modifies the result of observed method and required to have a return vlue. This methods name will be same as observed method with prefix “after”. e.g: afterObservedMethod.

Through below example I am going to modify result value.

Around:

This methods runs on both before and after the observed method. This methods allows you completely override the observed method. This methods name will be same as observed method with prefix “around”. e.g: aroundObservedMethod.

Before the list of original method call ,around method call callable to allow a call to next method in chain and if not call to callable then it will prevent execution of next method in chain and original method call.

As you can see both “before , after” have same format to modify a observed method but “around” has a little different like “callable” which does the work of prevent and allow to execute the next method in chain. You can use same argument in your plugin class of observed class’s argument but if argument is assigned null value in observed method then just simple use the argument in your plugin class, because plugin never accept null.

For example a method “save” like below and you want rewrite by plugin then:

but in your plugin class you can use like:

Not like “public function aroundSave(\Magento\Catalog\Model\Product $subject, callable $proceed, anyType $obj = null)“. If you will use like this then PHP will through fatal error.

Prioritizing your plugins:

“Before” to “around”:

The plugin class you created depends upon the “sortOrder” attribute/property. All plugins will be executed depending upon lowest to highest “sortOrder” prior to execution of observed method. So before methos will execute first and then around will be executed depending upon “sortOrder”.

“Around” to “Before”:

The plugin class you created depends upon the “sortOrder” attribute/property. All plugins will be executed depending upon highest to lowest “sortOrder” prior to execution of observed method. So “around” method will execute first and then “after” will be executed depending upon “sortOrder”.

Flow example:

 

magento 2 plugin Interceptors system
magento 2 plugin ordering of modules depending upon sortOrder

 

magento 2 plugin Interceptors system
magento 2 plugin sort order of modules on dispatching

 

 

 

Hope you may get clear on “Magento 2 plugin Interceptors system”. Thanks for visiting and see you again on another article.

Get your full code from here Source Code

 

Magento 2 plugin Interceptors system
Tagged on:                     

Leave a Reply

Your email address will not be published.