Magento 2 Plugin’s XML

Overview

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. This allows you to substitute or extend the behavior of original, public methods for any class or interface.

Declaring the XML

A plugin for a class object can be declared in the di.xml file in your module:

<config>
    <type name="{ObservedType}">
        <plugin name="{pluginName}" type="{PluginClassName}" sortOrder="1" disabled="true"/>
    </type>
</config>
You must specify these elements:
  • type name. 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\<ModelName>Plugin.

The following elements are optional:

  • 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.