How to Design Complex Solutions Using the Plugins in Magento 2

Jun 12, 2018259Gennadiy Haritonov
How to Design Complex Solutions Using the Plugins in Magento 2

Magento 2 Plugin (Interceptor) is a class that allows you to change the behavior of other classes’ methods by calling custom code before, after, or instead of calling specific methods. This reduces the probability of conflicts between different code sections representing the same functionality.

Plugin Configuration

, where:

{ObservedType} — the name of the class, whose method is to be changed;

{PluginName} — the name of the plugin;

{PluginClassName} — plugin’s class name;

sortOrder — plugin’s calling order;

disabled — the ability to make Magento ignore the plugin while leaving its configuration in place.

Plugin Methods

Before Methods

Before methods allow modifying the target class method arguments before this method is called.

The name of the plugin method is concatenated ‘before‘ and the name of the method, whose arguments are to be changed. The first argument of this method is the class whose method is called. The remaining arguments must match the arguments of the called method, including default values.

Before methods must return an array of arguments.

After Methods

After methods allow modifying the result of the target method.

The first argument is the target class instance (like in before methods), the second is the return value of the original method.

Around Methods

Around methods allow you to execute code both before and after the target method in one place.

$proceed argument is PHP closure, which in turn calls the target method.

Such methods allow you to completely replace the target method.

Limitations

Unlike Magento 1 class rewrites, plugins do not inherit the target class, which facilitates the peaceful coexistence of several plugins that change the same method. But for the same reason, plugins have certain limitations.

Plugins cannot be used with:

  1. Final methods and classes
  2. Protected/private methods
  3. Static methods
  4. __construct methods
  5. Virtual types
  6. Objects that are instantiated before Magento\Framework\Interception is bootstrapped

Plugin Sorting

Plugin sortOrder parameter allows you to determine in which order the plugin methods will be called in case multiple plugins are observing the same method.

Plugin1 Plugin2 Plugin3
sort order 10 20 30
before beforeMethod() beforeMethod() beforeMethod()
around aroundMethod() aroundMethod()
after afterMethod() afterMethod() afterMethod()

In this case, the processing of plugins will occur in the following order:

  1. Plugin1::beforeMethod()
  2. Plugin2::beforeMethod()
  3. Plugin2::aroundMethod() (Magento calls the first half until callable)
    1. Plugin3::beforeMethod()
    2. Plugin3::aroundMethod() (Magento calls the first half until callable)
      • TargetClass::method()
    3. Plugin3::aroundMethod() (Magento calls the second half after callable)
    4. Plugin3::afterMethod()
  4. Plugin2::aroundMethod() (Magento calls the second half after callable)
  5. Plugin2::afterMethod()
  6. Plugin1::afterMethod()

That is it regarding plugins in Magento 2, hope it was helpful. If you have any questtions on the article, please leave them in the comment section below.



Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *