Magento 2 plugins: What are they and how to use them?

Magento 2 plugins (Interceptor) is a class that helps you to change the behavior of other classes’ methods by calling custom code before, after, or instead of calling specific methods. Interception Plugin is considered as a little Magento 2 extension that permits editing the behavior of any public class or method by intercepting a feature call and running code either before or after or around the function call. With the help of the Magento 2 Plugin Interception, you can adjust the behavior of a class while there is no need to change the class straight.

What are the Magento 2 plugins?

Magento 2 Plugins help you to customize a single method which it can be called before, after, and around the method. To prevent conflicts, plugins for the same class are accomplished in sequence based on their sort order. For a module developer, Magento 2 plugins bring some benefits:

  • Forwarding any method call that is utilized on an object manager controlled object and taken programmatic action
  • Modifying the return value of any method call that is utilized on an object manager controlled object
  • Adjusting the arguments of any method call that is used on an object manager controlled object
  • Proceeding likewise when other modules are in progress of the same method in the same or predictable way.

However, it comes with limitations

Magento 2 plugins limitation

For the same reason, plugins have certain limitations. Magento 2 Interception plugin doesn’t work with:

  • Objects that are instantiated before Magento\Framework\Interception is bootstrapped
  • Final methods
  • Final classes
  • Any class that contains at least one final public method
  • Non-public methods
  • Class methods (such as static methods)
  • __construct
  • Virtual types

How to use Before, Around & After plugins

In Magento 2 plugins are used 3 types of interceptors:

  • Before Plugin
  • After Plugin
  • Around Plugin

Before plugin

Before methods are the first ways to run in an observed method. These methods allow modifying the target class method arguments before this method is called. To use the before methods for changing the arguments of an observed method, you need to return a modified argument. If there are various arguments, the returning will be carried out according to a range of those arguments.

Namespace Vendor\Module\Plugin;
class Plugin
{
 public function beforeMethodName(\Vendor\Module\Model\TargetClass $subject, $arg1, $arg2, $arg3 = null)
 {
 return [$arg1, $arg2, $arg3];
 }
}

After plugin

After methods start running right after the observed method is completed. After methods have responsibility for editing the results of an observed method in the correct way and being asked to get a return value. Because Magento 2 after plugins also gain the access to input parameters. The ‘after’ prefix must be included in the name of this method. For example:

Namespace Vendor\Module\Plugin;
class Plugin
{
public function afterMethodName(\Vendor\Module\Model\TargetClass $subject, $result)
{
return $result;
}
}

Around plugin

Around methods enables the code to run before and after the observed method, so you can override a method. The ‘around’ prefix is included in the name of this method. For example:

Namespace Vendor\Module\Plugin;
class Plugin
{
public function aroundMethodName(\Vendor\Module\Model\TargetClass $subject, callable $proceed, $arg1, $arg2, $arg3)
{
$result = $proceed();
return $result;
}
}

Around methods enables to the execution of 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 help you to completely replace the target method.

How to disable Magento 2 plugins?

Magento 2 plugins can be disabled in a di.xml file. To disable a plugin, set the disabled parameter of the plugin declaration to true.

<type name="Magento\Checkout\Block\Checkout\LayoutProcessor">
 <plugin name="ProcessPaymentConfiguration" disabled="true"/>
</type>

When ProcessPaymentConfiguration is the name of the plugin declared in the vendor/magento/module-payment/etc/frontend/di.xml.

Remember that the same class can be called two ways: with the leading slash or without.

\Magento\Checkout\Block\Checkout\LayoutProcessor

and

Magento\Checkout\Block\Checkout\LayoutProcessor

are both valid.

Winding-up

When you want to disable the plugin, you need to use the same path format to call and disable the plugin. That is it regarding plugins in Magento 2, hope it was useful for you. Do not hesitate to contact us if you have any doubts about Magento 2 plugins

Besides, if you are looking out for a cost effective Magento package for your eCommerce store, then look nowhere other than Magesolution. We not only offer an affordable Magento Development Package for all size and budget but also ensure that it helps your online business grow and sustain. Contact us for a free consultation!