Magento 2 Plugins

- Magento 2

Magento 2 Plug-ins

Although such terms as modules, plugins, and extensions are often used as synonyms, they also have slightly different meanings, and today we are gong to draw your attention to Magento 2 plugins. As for extensions and modules, they are described here: The Best Magento 2 Extensions.

In Magento 2, plugins are used for changing the behavior of any original public method. Moreover, you can implement enhancements in any Magento class via the Plugin class. To apply changes properly, you must follow declaration and naming rules. Implement interception, used for reducing conflicts among extensions, with the help of the Plugin class, and note that every plugin is utilized for changing the behavior of an appropriate class, but not for changing a class itself.

Magento 2 Plugin limitations

Although Magento 2 plugins offer relatively wide functionality, they also have some limitations. For instance, it is impossible to use them with final methods and classes. Non-public methods are not supported as well. Besides, Magento 2 plugins does not work in static methods and other class methods. The same s about inherited methods, __construct, and virtual types.

How to declare Magento 2 plugin

To declare a Magento 2 plugin, go to a module’s di.xml file and specify the following elements:

Where:

  • type name is something observed by the Magento 2 plugin. It can be a class, a virtual type, or an interface.
  • plugin name is an element that identifies a Magento 2 plugin. Being an arbitrary name, it is also utilized for merging plugin’s configurations.
  • plugin type illustrates a name of a plug-in’s class, besides it can define its virtual type. The element requires the following schema to be utilized: ‘\Vendor\Module\Plugin\<ModelName>Plugin’
  • plugin sortOrder. Chances are, you have several Magento plugins that call the same method. Thus, you should set the order in which they run. This element should be used.
  • plugin disabled. If you need to disable your plugin, utilize this element – set it to true.

How to prioritize Magento 2 plugins

There are several conditions that provide influence on how Magento 2 plugins apply to the same interface or class:

1. Since a listener method can be applied before, after, or around an appropriate original method, you can extend an original method’s behavior with the aid of one or several following methods :

  • Utilize the before-listener to improve the original method’s arguments.
  • Utilize the after-listener to improve values that an original method returns.
  • Utilize the around-listener to improve both.
  • Besides, you can override an original method which is a conflicting change. Note that extending a class’ behavior is a non-conflicting change.

2. Another thing that impacts on the overall Magento 2 plugin’s behaviour is a sort order  in which plugins are run. If several Magento 2 plugins apply to the same method, the following sequence is utilized:

  • a Magento 2 plugin which has the highest priority – the before listener;
  • a Magento 2 plugin which has the highest priority – the around listener;
  • in case of other before listeners, the specified sort order is utilized from the smallest value to the greatest one.
  • in case of other around listeners, the specified sort order is utilized: from the smallest value to the greatest one.
  • a Magento 2 plugin which has the lowest priority – the after listener.
  • in case of other after listeners, the reverse sort order is utilized: from the greatest value to the smallest one.

Note that the highest priority means the smallest value of the sortOrder argument and the lowest priority means the greatest value of the argument.

Example Magento 2 plugins

Below, we describe how to use Magento 2 plugins. For example, you should utilize the before-listener method, if you want to change original method’s arguments or add a new behavior before calling an original method. Just prefix the name of your original method with before as shown in the following example:

Also, you can rely on the after-listener method for changing values returned by your original method or adding some behavior after calling the original method. This time, you should use the after prefix:

As you might have guessed, the around-listener method is used for changing the arguments and returned values or adding some new behavior both before and after calling the original method. The original listener should be prefixed with around:

Note that there are $subject and $proceed parameters the around-listener method receives. Both are followed by the arguments which belong to the original method. The $subject parameter is used for providing access to all public methods of your original class, while the $proceed parameter is utilized for calling the next Magento 2 plugin or method.

Configuration inheritance of Magento 2 plugins

Due to the configuration inheritance, a Magento 2 plugin applied to an interface or a class is derived by interfaces or classes that utilize this original class or interface. Hence, it is possible to rely on the configuration inheritance while implementing AOP-like functionality based on plugins. To override plugins from the global scope, change the di.xml file of an appropriate area.

How to change core functionality in Magento 2 (via plugins)

Fundamentals of Extending Magento 2 – php[world] 2015

For further information about Magento 2 plugins, check this posts: