Migrate to Magneto 2 now. Go live in 3 weeks from our team of Magento certified professionals.
We have been actively blogging on the new features and functionalities in Magento 2. In that series, this blog is about Magento 2 plugins.Here we discuss the plugin structure and how to create a plugin.
Magento 2 Plugin
- Plugin is a class which changes the behavior of an original method in Magento code.
- Magento 2 introduces interception concept which avoid conflicts between extensions, which changes the behavior of the same class or method.
- Plugin uses before, after, or around an original method.
Listeners in Plugin
- beforemethodname() -> to change the arguments of an original method or add some behavior before an original method is called, use the before-listener method. The before prefix should be added to the name of an original method.
- aftermethodname() -> to change the values returned by an original method or add some behavior after an original method is called, use the after-listener method. The after prefix should be added to the name of an original method.
- aroundmethodname() -> to change both the arguments and returned values of an original method or add some behavior before and after an original method is called, use the around-listener method. The around prefix should be added to the name of an original method.
Declaring a Plugin
A plugin can be declared in di.xml file. The declaration syntax for plugin is
type name -> a class, interface, or virtual type, which is observed by a plugin.
plugin name -> an arbitrary plugin name
plugin type -> the name of a plugin’s class or virtual type
plugin sortOrder -> order, in which the plugins calling the same method are to be executed.
plugin disabled ->to disable a plugin, the value of the element should be set to ‘true.’
Sample Plugin from module Catalog -> di.xml
“clean(MagentoLogModelLog $object)” in type MagentoLogModelResourceLog is overwritten by the method “afterClean(MagentoLogModelResourceLog $subject, $logResourceModel)” found in Plugin file “MagentoCatalogModelPluginLog”.
MagentoLogModelResourceLog.php file
MagentoCatalogModelPluginLog.php file
Creating a Plugin
Please refer the below blog for creating a module.
https://www.dckap.com/blog/magento-2-module-development
After creating a module, please follow the below steps for creating a plugin.
Step 1: Create the Block file app/code/Dckap/Welcome/Block/Welcome.php
Step 2: In /app/code/Dckap/Welcome/view/frontend/templates/ create a template file welcome.phtml
Step 3: Create the file di.xml inside app/code/Dckap/Welcome/etc/
Add the below lines in di.xml
Step 4: Create a model file Welcome1.php inside the path app/code/Dckap/Welcome/Model
Please take a look on ‘Hello World’ text displayed in the front end of the module.
Step 5: Create a Plugin folder inside Model folder of the module.
File Welcome2.php needs to be created inside that Plugin folder.
The beforeDckModelWelcome method in the Welcome2.php file appends ‘Welcome to ‘ text with ‘Hello World‘ text of DckModelWelcome method present inside Welcome1.php file.
Limitations
Plugin does not apply to following Scenario
- Classes created without dependency injection
- Final methods
- Final classes
As Magento 2 is coming, it’s a good idea to become familiar with its various new features and functionalities instead of waiting until the last hour.
Hopefully this blog is a good motivation and/or introduction to get started with Magento 2 plugin development.