Plugins: Interceptor in Magento 2
Table of Contents:
- What Are Magento 2 Plugins
- How Many Types Of Magento 2 Plugins
- Magento 2 plugins limitation
- Why should you use Interceptor or Plugins in Magento 2
- Defining a plugin
- Conclusion:
What are the Magento 2 plugins
In Magento 2 you can create plugins (interceptors) that allow you to extend functionality and execute your own code before, after, or around any PHP class public method
To prevent conflicts, plugins for the same class are accomplished in sequence based on their sort order. For a module developer, Magento 2 plugins bring some benefits:
- Forwarding any method call that is utilized on an object manager controlled object and taken programmatic action
- Modifying the return value of any method call that is utilized on an object manager controlled object
- Adjusting the arguments of any method call that is used on an object manager controlled object
- Proceeding likewise when other modules are in progress of the same method in the same or predictable way.
How many types of Magento 2 plugins
There are 3 types of Magento 2 plugins:
- Before Plugin.
- After Plugin.
- Around Plugin.
1.Before method is run first before the call to an observed method. Note that these methods must be named the same as the observed method with the prefix ‘before’.
By returning a modified argument, you can utilize before methods to modify the arguments of an observed method. The method should return an array of parameters if there are any. It should return a null result if the method does not change the parameter for the observed method.
2.After method is run after the observed method is completed. These methods must be named after the observed procedure and have the prefix ‘after’.
By altering the initial result and returning it at the end of the method, you can change the outcome of an observed method.
3.Around method is run before and after its observed method. The name of the around method must be the same as the observed method, with the prefix ‘around’.
You can override an observed method by using these methods.
Magento 2 plugins limitation
For the same reason, plugins have certain limitations. Magento 2 Interception plugin doesn’t work with:
- Objects that are instantiated before Magento\Framework\Interception is bootstrapped
- Final methods
- Final classes
- Any class that contains at least one final public method
- Non-public methods
- __construct and __destruct
- Class methods (such as static methods)
- Virtual types
Why should you use Interceptor or Plugins in Magento 2
- Minimum confliction among extensions that change the behavior of the same class or method.
- We can prevent collisions between plugins by using sort order attribute of plugin. Plugin can be called sequentially according to a sort order, so it does not conflict with other plugin class.
- No issue of rewriting the system.
- Customize the same method in different modules.
- Ability to modify the return value of any method call that is used on an object manager controlled object.
- Ability to modify the arguments of any method call that is used on an object manager controlled object.
- Plugin class does not change class itself but it only modifies the behavior of class function.
Defining a plugin
By applying code before, after, or around a public method, a plugin extends or modifies that method’s behavior.
The first argument for the before, after, and around methods is an object that provides access to all public methods of the observed method’s class.
Conclusion
In conclusion, Plugins, specifically the Interceptor functionality, play a vital role in extending and customizing the Magento 2 platform. They provide developers with a powerful tool to modify the behavior of existing classes and methods without directly modifying their code. This allows for seamless customization and enhancement of core functionalities while maintaining the integrity of the original codebase.
Plugins, developers can easily insert additional logic before, after, or around existing methods, enabling them to add new features, manipulate data, or perform validations. This flexibility not only streamlines the development process but also ensures that future updates and upgrades to the Magento platform can be seamlessly integrated without conflicts.
Overall, Plugins: Interceptor in Magento 2 empowers developers to extend the capabilities of the platform, customize functionalities, and build unique solutions to meet specific business requirements. By leveraging this powerful mechanism, developers can create highly flexible and scalable e-commerce applications while keeping the core codebase intact, ensuring a robust and future-proof solution.