- 657
- 15
- 5
Plugins (interceptors) extension is intended for Magento developers only. As developers know, there are 2 main means to change existing functionality in Magento 1, namely events and rewrites.
• <events> - a proper/recommended approach, but ‘events’ are not always available where they are needed.
• <rewrite> - not a better solution since there might be several extensions fighting for the same overrided class, which often leads to a conflict between extensions.
• <plugin> - a new solution we have developed for everyone and for free. Plugins (interceptors), similarly to Magento 2, helps you avoid conflicts, rewrites and makes development process more flexible. This extension is going to bring Magento 1 to life. Again.
Description
A plugin, or interceptor, is an XML-construction in config.xml of extension that enables a developer to modify the behavior of class functions. This is achieved by intercepting a function call and running code before, after or around that function call. This allows a developer to substitute or extend the behavior of original methods for any blocks, helpers and models.
This interception approach reduces conflicts among extensions that change the behavior of the same class or method. What is it that makes these plugins work with one another without conflicts? The interceptors can be called one by one in accordance with a specified sort order.
The extension enables intercepting public and protected methods. Obviously, the final and private methods as well as final classes are not supported.
Implementation Example
Plugin syntax is similar to that of both rewrite and events. Add the code below in the current module’s ModuleName/etc/config.xml:<!-- Example config.xml for blocks --> <config> <global> <blocks> <!-- Module: catalog --> <catalog> <plugin> <!-- Block: catalog/product_list --> <product_list> <!-- Plugin handle name --> <!-- (any name can be used, but following rules is advised): --> <!-- [vendor_name]_[module_name]_[method_name]_[before|around|after] --> <safemage_pluginexample_after_to_html_after> <!-- Variants: before, around or after --> <type>after</type> <method>_afterToHtml</method> <!-- Run plugin model: safemage_pluginexample/plugin --> <!-- and method: afterAfterToHtml --> <run>safemage_pluginexample/plugin::afterAfterToHtml</run> <!-- Optional parameters: --> <sort_order>10</sort_order> <disabled>0</disabled><!-- Values: 1 or 0 --> </safemage_pluginexample_after_to_html_after> </product_list> </plugin> </catalog> </blocks> </global> </config>
ModuleName/Model/Plugin.php file needs to be created (like Observer.php for events):
<?php // Example plugin class class SafeMage_PluginExample_Model_Plugin { /** * @param Mage_Catalog_Block_Product_List $object * @param string $result html * @param array $arguments * @return string */ public function afterAfterToHtml($object, $result, array $arguments) { return '<div>Mode: ' . $object->getMode() . '</div>' . $result; } }
To download more examples, click on the link: SafeMage_PluginExample.zip
How it works
The extension includes a setting:• System > Config > Advanced > System > Plugins (Interceptors)
where one can see a full list of plugins and their sort order, and enable/disable all plugins.
For each class (for example, Mage_Catalog_Block_Product_List) the plugins work with, a new file of Plugged-class is being generated located in: MagentoRoot/var/cache/plugins/Mage_Catalog_Block_Product_List_Plugged.php
<?php // Example new plugged class class Mage_Catalog_Block_Product_List_Plugged extends Mage_Catalog_Block_Product_List { protected function _afterToHtml($html) { $arguments = array($html); // CURRENT METHOD $result = parent::_afterToHtml($html); // AFTER $result = Mage::getSingleton('safemage_pluginexample/plugin')->afterAfterToHtml($this, $result, $arguments); return $result; } }
This Plugged-class is performed autoload() and used by Magento instead of the original Mage_Catalog_Block_Product_List to call plugins upon affected methods.
With cache disabled, the file is generated anew when the target class has been addressed. But with Cache Type = Configuration enabled, the file is generated only once and then is being pulled from cache, which doesn’t drain your system’s performance, until this cache type has been refreshed (or Flush All Cache has been activated).
Features
- Speeds up development process
- Works with blocks, helpers and models
- Runs code before, after or around that method call
- Adds plugins to public and protected methods
- Ability to enable/disable plugins
- Preview listing of all plugins and their sort order
- Does not affect Magento performance
- Works with/without Magento cache running
- Compatible with Compilation Mode
Compatibility
Magento CE 1.4.x, 1.5.x, 1.6.x, 1.7.x, 1.8.x, 1.9.xMagento EE 1.9.x, 1.10.x, 1.11.x, 1.12.x, 1.13.x, 1.14.x
Changelog
new feature bugfix- v.1.0.3 (Jan 3, 2017)
- Fixed a bug with class parameters.
- v.1.0.2 (Dec 22, 2016)
- Fixed a bug with plugged class autoload.
- v.1.0.1 (Dec 10, 2016)
- Fixed a bug with array function parameters.
- v.1.0.0 (Dec 1, 2016)
- Initial release.
I wish I could ‘intercept’ private methods.