A talk I gave at Meet Magento Belgium 2017: https://be.meet-magento.com/presentation/manipulating-magento-make-want/
How can you make Magento do what you want? In Magento 2 there are a lot of ways to add customizations. What are your options? Can you do a rewrite like in Magento 1? Should you use an event to add that business-critical logic when your invoice reaches state paid? Or is a plugin a better option? Maybe it’s the only option. What’s the deal with dependency injection and why do I need interfaces? In this talk we’re going to explore all the possibilities.
6. Dependency injection
“Dependency injection is the concept of the
external environment injecting dependencies for
an object instead of that object manually
creating them internally.”
23. Not calling the callable
✖Other around plugins (sort order)
✖Other after plugins (sort order)
✖Original method
24. Call order prior original
1. Sort order from low to high
2. Order of the uasort() array
3. Before plugins
4. Around plugins (before calling the callable)
25. Call order post original
1. Sort order from high to low*
2. Reversed order of the uasort() array
3. Around plugins (after calling the callable)
4. After plugins
* Only if an around plugin exists
26. PHPro/AModule/etc/di.xml à after plugin
<type
name="MagentoStoreModelAddressRenderer">
<plugin name="renderer_a"
type="PHProAModulePluginRenderer"
sortOrder="10"/>
</type>
PHPro/ZModule/etc/di.xml à after plugin
<type
name="MagentoStoreModelAddressRenderer">
<plugin name="renderer_z"
type="PHProZModulePluginRenderer"
sortOrder="20"/>
</type>
39. Dispatch order
1. Observers in the global area
a. Module sequence order
b. Order in events.xml
2. Observers in adminhtml or frontend area
a. Module sequence order
b. Order in events.xml
43. Preferences
• (+) Replace entire class or interface
implementation
• (-) Only one preference can be active
• (-) Replace entire class to replace one line of
code
45. Plugins
• (+) Manipulate behavior before, after and
around one method
• (+) Access to arguments and output
• (+) Multiple plugins for one method
• (-) Only public methods
Who am I?
Who is this presentation for?
What made me make this?
The Magento 1 “rewrite”
PHPro_ZModule
Brand new in Magento 2. Better than the Magento 1 rewrite.
Name: unique in the Magento installation
Type: instance of your plugin
Disabled: default false
SortOrder
This will prevent the executing of other plugins for the same method depending on the sort order of those plugins. The original method will also not be executed.
#2 Could be the sequence order of the modules or the area (global or adminhtml/frontend) but I got inconclusive tests. Uasort is used and this does not keep the original order for two items that are equal
renderer_z
Already in Magento 1
Name: unique in the Magento installation (required)
Instance: your observer instance (required)
Disabled: default is false
Shared: default is false, true = singleton, just one instance for this request