Hello Everyone!
Magento2 Plugins
Rahul Mahto
Magento Developer
What is
Plugin?
➢Plugin is way to change
behaviour of a class without
changing the class.
➢Plugin allow us to change
behaviour of method of a
class.
➢Plugin is simply a design
pattern called interception.
Why Plugin?
➢It overcome the issues of
Rewriting System.
➢By plugin we can customize
same method in different
modules.
Request for object Class A Instance of Class A
Request for object Class A Instance of Class BClass B
Rewrited by
Normal Class flow. Requesting object for Class A and getting Instance of Class A.
Rewrited Class flow. Requesting object for Class A and getting Instance of Class B.
Request for object of Class A
Is class is
rewrited by
some Class
B?
Create instance of class B
Create instance of class A
No
Yes
Return created instance
END
START
Rewrited Class Flow
Types of
Plugin?
In Magento2 we can use plugin in 3
ways.
➢Using before listener
➢Using after listener
➢Using around listener
Declare a
Plugin
Plugin is called by adding prefix
‘before’, ‘after’ or ‘around’ to the
method name and setting first letter
of original method to capital.
Original method Name - getName()
Plugin Method Name -
beforeGetName(), afterGetName()
or aroundGetName()
Define plugin in module’s di.xml file
<config>
<type name="{ObservedType}">
<plugin name="{pluginName}" type="{PluginClassName}" sortOrder="1"
disabled="true"/>
</type>
</config>
type name - A class which the plugin observes.
plugin name - Name that identifies a plugin.
type - The name of a plugin’s class.
Optional Elements:
sortOrder - The order in which plugins that call the same method are run.
disabled - To disable a plugin
Before
Listener
➢Used to change arguments of
method.
➢Used to add some behavior
before an original method is
called.
➢Do not need to have a return
value.
**Logos and Trademarks are owned by
their respective owners
After
Listener
➢Used to change values returned
by an original method.
➢Used to add some behavior
after an original method is
called.
➢Do not need to have a return
value.
**Logos and Trademarks are owned by
their respective owners
**Logos and Trademarks are owned by
their respective owners
Around
Listener
➢Used to change both the
arguments and the returned
values of an original method.
➢Used to add some behavior
before and after an original
method is called.
➢Must have a return value.
Behind the
Scene
➢Interceptor class is generated
➢Extended by original class
➢It contains all public method
➢It checks for plugin in methods
➢Process all plugins
➢Return result
Request for object of Class A
Does class
contain
Plugin?
Generate Interceptor Class for A.
Return Instance of Class A
No
Yes
Return Instance of Interceptor
Class
END
START
Plugin System Flow
Request a Method
Does this
method
contain
Plugin?
Process Plugins
Return Result
No
Yes
Return Result
END
START
Plugin System Flow
Method in Original Class of Product Model in Magento2
Method in Interceptor Class of Product Model in Magento2
Limitations
➢Objects instantiated before
MagentoFrameworkIntercept
ion
➢Final methods and Final
classes
➢Non-public methods
➢__construct
➢Virtual types
Thank You!

Magento Meetup New Delhi- Magento2 plugins

  • 1.
  • 2.
  • 3.
    What is Plugin? ➢Plugin isway to change behaviour of a class without changing the class. ➢Plugin allow us to change behaviour of method of a class. ➢Plugin is simply a design pattern called interception.
  • 4.
    Why Plugin? ➢It overcomethe issues of Rewriting System. ➢By plugin we can customize same method in different modules.
  • 5.
    Request for objectClass A Instance of Class A Request for object Class A Instance of Class BClass B Rewrited by Normal Class flow. Requesting object for Class A and getting Instance of Class A. Rewrited Class flow. Requesting object for Class A and getting Instance of Class B.
  • 6.
    Request for objectof Class A Is class is rewrited by some Class B? Create instance of class B Create instance of class A No Yes Return created instance END START Rewrited Class Flow
  • 7.
    Types of Plugin? In Magento2we can use plugin in 3 ways. ➢Using before listener ➢Using after listener ➢Using around listener
  • 8.
    Declare a Plugin Plugin iscalled by adding prefix ‘before’, ‘after’ or ‘around’ to the method name and setting first letter of original method to capital. Original method Name - getName() Plugin Method Name - beforeGetName(), afterGetName() or aroundGetName()
  • 9.
    Define plugin inmodule’s di.xml file <config> <type name="{ObservedType}"> <plugin name="{pluginName}" type="{PluginClassName}" sortOrder="1" disabled="true"/> </type> </config>
  • 10.
    type name -A class which the plugin observes. plugin name - Name that identifies a plugin. type - The name of a plugin’s class. Optional Elements: sortOrder - The order in which plugins that call the same method are run. disabled - To disable a plugin
  • 11.
    Before Listener ➢Used to changearguments of method. ➢Used to add some behavior before an original method is called. ➢Do not need to have a return value.
  • 14.
    **Logos and Trademarksare owned by their respective owners
  • 15.
    After Listener ➢Used to changevalues returned by an original method. ➢Used to add some behavior after an original method is called. ➢Do not need to have a return value.
  • 18.
    **Logos and Trademarksare owned by their respective owners
  • 19.
    **Logos and Trademarksare owned by their respective owners
  • 20.
    Around Listener ➢Used to changeboth the arguments and the returned values of an original method. ➢Used to add some behavior before and after an original method is called. ➢Must have a return value.
  • 22.
    Behind the Scene ➢Interceptor classis generated ➢Extended by original class ➢It contains all public method ➢It checks for plugin in methods ➢Process all plugins ➢Return result
  • 23.
    Request for objectof Class A Does class contain Plugin? Generate Interceptor Class for A. Return Instance of Class A No Yes Return Instance of Interceptor Class END START Plugin System Flow
  • 24.
    Request a Method Doesthis method contain Plugin? Process Plugins Return Result No Yes Return Result END START Plugin System Flow
  • 26.
    Method in OriginalClass of Product Model in Magento2
  • 27.
    Method in InterceptorClass of Product Model in Magento2
  • 29.
    Limitations ➢Objects instantiated before MagentoFrameworkIntercept ion ➢Finalmethods and Final classes ➢Non-public methods ➢__construct ➢Virtual types
  • 30.