Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take 2 weeks Free Trial! Try Now

Tutorial : Plugin System in Magento 2

Here, The Magneto development services provider will explain to you the plugin system in Magento 2. You will learn the way of how to use Magento 2 plugins from the basics level. Read and discover how professionals do it.

You will learn through this article how to use Magento 2 plugins that modify the behavior of all public functions without overloading the PHP class (block, model, etc.) of the container.

What are plugins?

Magento 2 plugins provide a mechanism to effectively manage this type of "rewriting" and to minimize conflicts between extensions to avoid having to override a class to modify a simple.

Limitations plugins

As of today, plugins can only be used to rewrite public methods and are so subject to the below limitations:

  • Final methods
  • Non-public methods (protected or private)
  • Static methods
  • Constructor ( _construct)
  • Virtual Types

Types plugin

Magento provides three types of different plugins:

1) before:

Use it to change the arguments provided to a method.

2) around:

Use is to change the behavior of a method.

3) after:

To "rework" the output of a method.

Declaration of a plugin

Any plugins must be declared in your module's file di.xml of your module. This file can be placed, depending on its use, either within the directory, etc. Your module (plugin to use the backend and frontend) or in etc/frontend

(limited use in frontend) or etc/adminhtml(limited use in backend):

<config> <typename=" {observer_type}"> <pluginname=" {plugin_name}" type="{plugin_type}" sortOrder="{plugin_order}" disabled="{plugin_disabled}" /> </type> </config>

The following can be specified:

1) observer_type:

The class name or virtual interface type you want to observe

2) plugin_name:

The name of the plugin (eg. catalog_product_custom_plugin)

3) plugin_type:

The name of the class or type used by the virtual module (ex.Vendor\Module\Plugin\ModelNamePlugin)

4) plugin_order:

The order in which the plugin will run. Very useful to effectively manage the execution order of several plugins overloading the same method (see "Execution Order" below for details).

5) plugin_disabled:

If you want to disable the plugin, set this parameter to true.

Execution order of the plugins

When multiple plugins rewriting the same method exist, the Magento will follow the following order of performance:

  • The type of plugin beforethat has the highest priority (= the one with the least sortOrder)
  • The type of plugin aroundthat has the highest priority (= the one with the least sortOrder)
  • The other type of plugins beforebased on their priority (from smallest to largest sortOrder)
  • The other type of plugins aroundbased on their priority (from smallest to largest sortOrder)
  • The type of plugin afterthat has the lowest priority (= the one with the largest sortOrder)
  • The other type of plugins afterbased on their priority (largest to smallest sortOrder)

Example plugins

before

To change the settings passed to a function, it is necessary to create a method before [methodName] in your plugin (which [methodName] corresponds to not the method you want to change, for example, SetName for a method named setName).

The method created within your plugin parameter must take the class that includes the function you want to change and its various parameters.

Example of method overloading setNameclass \Magento\Catalog\Model\Product:

<?php namespaceMy\Module\Plugin; classProductPlugin { publicfunctionbeforeSetName(\Magento\Catalog\Model\Product $subject, $name) { return['('. $name. ')']; } }

In this example, the product name passed in the argument $name will be automatically enclosed in parentheses.

As you have probably noticed, the type of plugins before has returned an array containing all the parameters of the function.

around

A method around [methodName] must be created within your plugin (which [methodName] corresponds to not the method you want to change, for example, Save for a method named save) to change the behavior of a function, a. The method created within your plugin parameter should take the class that includes the function you want to change and another type parameter \Closurecorresponding to the parent method.

Example of method overloading saveclass \Magento\Catalog\Model\Product:

<?php namespaceMy\Module\Plugin; classProductPlugin { publicfunctionaroundSave(\Magento\Catalog\Model\Product $subject, \Closure $proceed) { $returnValue= $proceed(); if($returnValue) { $this->doSomething(); } return$returnValue; } }

In this example, we execute line 9 that will retrieve the original method and the return value. If it is defined, a third function, called here do Something, will be executed. Finally, we return line 13, returning the original method.

after-

It is necessary to create a method after[methodName] in your plugin (which [methodName] corresponds to not the method you want to change, for example, GetName for a method named getName) to change the return of a function.

The method created within your plugin parameter must take the class that includes the function you want to change. And a variable $resultthat will contain the result of the original method.

Example of method overloading getNameclass \Magento\Catalog\Model\Product:

publicfunctionafterGetName(\Magento\Catalog\Model\Product $subject, $result) { return'|'. $result. '|'; }

In this example, the return of the original method, which is here in the name of the product, will be surrounded by "|". Magento developers from India have shared the definition, limitations, and uses of Magento 2 plugins for reference purposes only. You can ask for more info on the Magento 2 plugins in your comments.

Software Development Team
Need Software Development Team?
captcha
🙌

Thank you!
We will contact soon.

Oops! Something went wrong.

Recent Blogs

Categories

NSS Note
Trusted by Global Clients