DEV Community

Amine Yaakoubi
Amine Yaakoubi

Posted on

Overriding in magento 2 using plugins

Introduction

In the world of Magento 2 development, there are various methods available for overriding classes and methods. One popular and straightforward approach is using plugins. Unlike class preferences, which can sometimes cause conflicts, plugins offer a way to extend methods without altering the class itself. By using plugins, you can customize the behavior of Magento classes without replacing them entirely.

In this comprehensive guide, we will explore the concept of overriding classes in Magento 2 using plugins. We'll discuss the different types of plugins, their configuration in di.xml files, and the methods they can be applied to. Whether you're a beginner or an experienced developer, this tutorial will provide you with the knowledge and techniques necessary to leverage the power of plugins effectively.

Understanding Plugins in Magento 2

Before we dive into the specifics of using plugins, let's take a moment to understand what they are and how they work in Magento 2. A plugin, also known as an interceptor, is a class that modifies the behavior of a public method in another class, referred to as the target class. Plugins rely on the concept of method interception, which allows them to execute custom code before, after, or around the original method.

Unlike class preferences, which completely replace the target class, plugins work by wrapping the original method with their own logic. This means that multiple plugins can be attached to a single method, and they will be executed in the order specified in the di.xml file. This flexibility allows developers to modify the behavior of Magento classes without the risk of conflicts.

Plugins are a powerful tool for extending the functionality of Magento 2. They provide a way to add custom logic to existing methods, without the need to modify core files. With plugins, developers can enhance the behavior of Magento classes, introduce new features, and even override methods from third-party extensions.

Configuring Plugins in di.xml

To use a plugin in Magento 2, you need to configure it in the di.xml file of your module. The di.xml file is responsible for defining the dependency injection configuration for your module, including the declaration of plugins.

The di.xml file can be located in two different paths, depending on the scope of the configuration. The global di.xml file is located in the module's etc directory, while the specific di.xml file is located in the etc/ directory, where represents the specific area of the application (e.g., frontend, adminhtml, etc.).

To configure a plugin, you need to specify the target class and the plugin class in the type section of the di.xml file. Here's an example of how the configuration looks:

<config>
    <type name="Magento\Catalog\Api\Data\ProductInterface">
        <plugin name="your_module_plugin_name" type="Your\Module\Plugin\Class" />
    </type>
</config>

Enter fullscreen mode Exit fullscreen mode

In this example, we are configuring a plugin for the ProductInterface class in the Magento\Catalog\Api\Data namespace. The name attribute specifies a unique name for the plugin, while the type attribute specifies the fully qualified name of the plugin class.

The Three Types of Plugin Methods

Plugins in Magento 2 can intercept methods in three different ways: before, after, and around. Each type of interception offers a different approach to extending the behavior of a method.

Before Method

The before method allows you to execute custom code before the original method is called. It receives the same arguments as the original method and can modify them if necessary. The return value of the before method should be an array containing the modified arguments.

Here's an example of a before method:

namespace Your\Module\Plugin;

class Class {
    public function beforeMethod(TargetClass $subject, $arg1, $arg2)
    {
        // Custom code to execute before the original method
        $arg1 = $arg1 + 10;

        // Return the modified arguments
        return [$arg1, $arg2];
    }
}

Enter fullscreen mode Exit fullscreen mode

In this example, the beforeMethod plugin intercepts the method in the TargetClass and adds 10 to the first argument. The modified arguments are then returned as an array.

After Method

The after method allows you to execute custom code after the original method has been called. It receives the same arguments as the original method, as well as the result of the original method. The return value of the after method should be the modified result.

Here's an example of an after method:

namespace Your\Module\Plugin;

class Class {
    public function afterMethod(TargetClass $subject, $result)
    {
        // Custom code to execute after the original method
        $result .= ' (Modified)';

        // Return the modified result
        return $result;
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the afterMethod plugin intercepts the method in the TargetClass and appends (Modified) to the result.

Around Method

The around method allows you to execute custom code both before and after the original method. It receives a callable object as an argument, which represents the original method. The around method can execute custom code before and after calling the original method by invoking the callable.

Here's an example of an around method:

namespace Your\Module\Plugin;

class Class {
    public function aroundMethod(TargetClass $subject, callable $proceed)
    {
        // Custom code to execute before the original method

        // Call the original method
        $result = $proceed();

        // Custom code to execute after the original method

        // Return the result
        return $result;
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the aroundMethod plugin intercepts the method in the TargetClass and executes custom code both before and after calling the original method using the $proceed callable.

Conclusion

Using plugins in Magento 2 is a powerful way to extend the functionality of classes without modifying their core code. By intercepting methods with before, after, or around logic, plugins allow developers to customize the behavior of Magento classes and introduce new features. With a proper understanding of how to configure and use plugins, you can enhance your Magento 2 projects and create more flexible and maintainable code.

In this guide, we've explored the concept of plugins, their configuration in di.xml files, and the three types of plugin methods: before, after, and around. Armed with this knowledge, you can confidently leverage the power of plugins in your Magento 2 development workflow. Happy coding!

Top comments (0)