Home » Magento, Magento 28 August 2016

Magento 2: Override/Rewrite Block, Model, Controller, Helper using Plugin & Preference

This article shows how you can override / rewrite Block, Controller, Model, and Helper in Magento 2.

This can be done by two ways:

1) using Preference
2) using Plugin

Preference is similar to class rewrite in Magento 1. There is always possibility of conflict when two or more custom modules try to rewrite/override same core class.

There is Plugin to address the limitation of Preference. With Plugin, we can execute our code before, after and around the core/target class’s function. We don’t replace the core/target class’s code/function. We just add some code before/after the core code. It’s somehow similar to event observer. We just observe the core/target class’s function and execute our code in-between the core/target class’s function. Multiple modules can use their own plugins to add their own code before/after/around the same core/target class’s function.

Using Preference

Here are the examples of rewriting Block, Model, Controller, and Helper class using Preference. I suppose the module name as YourCompany_YourModule.

We have to define preference in in app/code/YourCompany/YourModule/etc/di.xml. There we define the core/target class which we want to rewrite. We also define our module’s class that is going to rewrite the core/target class.

BLOCK OVERRIDE

app/code/YourCompany/YourModule/etc/di.xml

Let’s rewrite getProduct() function of class Magento\Catalog\Block\Product\View. We will just log some message on var/log/debug.log for this test.

app/code/YourCompany/YourModule/Block/Catalog/Product/View.php

The log message is saved something like below in var/log/debug.log:

[2016-07-04 15:04:21] main.DEBUG: Block Override Test {“is_exception”:false} []

MODEL OVERRIDE

app/code/YourCompany/YourModule/etc/di.xml

Let’s rewrite getName() function of class Magento\Catalog\Model\Product. We will just log some message on var/log/debug.log for this test.

app/code/YourCompany/YourModule/Model/Catalog/Product.php

The log message is saved something like below in var/log/debug.log:

[2016-07-04 15:17:22] main.DEBUG: Model Override Test {“is_exception”:false} []

CONTROLLER OVERRIDE

app/code/YourCompany/YourModule/etc/di.xml

Let’s rewrite execute() function of class Magento\Catalog\Controller\Product\View. We will just log some message on var/log/debug.log for this test.

app/code/YourCompany/YourModule/Controller/Product/View.php

The log message is saved something like below in var/log/debug.log:

[2016-07-04 15:30:36] main.DEBUG: Controller Override Test {“is_exception”:false} []

HELPER OVERRIDE

app/code/YourCompany/YourModule/etc/di.xml

Let’s rewrite getProduct() function of class Magento\Catalog\Helper\Data. We will just log some message on var/log/debug.log for this test.

app/code/YourCompany/YourModule/Helper/Catalog/Data.php

The log message is saved something like below in var/log/debug.log:

[2016-07-04 15:42:54] main.DEBUG: Helper Override Test {“is_exception”:false} []

Using Plugin

Plugin observes the core/target class’s function and executes some code before and after the observed function. Just like Preferences, plugins are also declared in etc/di.xml file.

Here are the examples of rewriting Block, Model, Controller, and Helper class using Plugin. I suppose the module name as YourCompany_YourModule.

BLOCK OVERRIDE

app/code/YourCompany/YourModule/etc/di.xml

Here, we use all the methods: before, after, and around methods. With around method, we can add directly add code both before and after the observed function. As you can see above, that we have observed core class Magento\Catalog\Block\Product\View. In the below plugin code, we observe the function getProduct() which is present in the class Magento\Catalog\Block\Product\View. At first the beforeGetProduct method will be executed. After that, aroundGetProduct method will be executed. Finally, afterGetProduct method will be executed. You can look into the var/log/debug.log and confirm the method execution sequence.

app/code/YourCompany/YourModule/Plugin/ProductPlugin.php

The log message is saved something like below in var/log/debug.log:

[2016-07-05 07:40:14] main.DEBUG: YourCompany\YourModule\Plugin\ProductPlugin::beforeGetProduct -111- 29 {“is_exception”:false} []
[2016-07-05 07:40:14] main.DEBUG: YourCompany\YourModule\Plugin\ProductPlugin::aroundGetProduct -333- 46 {“is_exception”:false} []
[2016-07-05 07:40:14] main.DEBUG: YourCompany\YourModule\Plugin\ProductPlugin::aroundGetProduct -444- 52 {“is_exception”:false} []
[2016-07-05 07:40:14] main.DEBUG: YourCompany\YourModule\Plugin\ProductPlugin::afterGetProduct -222- 38 {“is_exception”:false} []

MODEL OVERRIDE

app/code/YourCompany/YourModule/etc/di.xml

Here, we use “before” and “after” method to execute code before and after the observed method getName($product).

app/code/YourCompany/YourModule/Plugin/ProductPlugin.php

CONTROLLER OVERRIDE

app/code/YourCompany/YourModule/etc/di.xml

Here, we use “around” method to execute code before and after the observed method execute present in class Magento\Catalog\Controller\Product\View.

app/code/YourCompany/YourModule/Plugin/ProductPlugin.php

The log message is saved something like below in var/log/debug.log:

[2016-07-05 07:50:28] main.DEBUG: YourCompany\YourModule\Plugin\ProductPlugin::aroundExecute -111- 60 {“is_exception”:false} []
[2016-07-05 07:50:29] main.DEBUG: YourCompany\YourModule\Plugin\ProductPlugin::aroundExecute -222- 66 {“is_exception”:false} []

HELPER OVERRIDE

app/code/YourCompany/YourModule/etc/di.xml

Here, we use “around” method to execute code before and after the observed method getProduct() present in class Magento\Catalog\Helper\Data.

app/code/YourCompany/YourModule/Plugin/ProductPlugin.php

The log message is saved something like below in var/log/debug.log:

[2016-07-05 07:58:27] main.DEBUG: YourCompany\YourModule\Plugin\ProductPlugin::aroundGetProduct -111- 75 {“is_exception”:false} []
[2016-07-05 07:58:27] main.DEBUG: YourCompany\YourModule\Plugin\ProductPlugin::aroundGetProduct -222- 81 {“is_exception”:false} []

Hope this helps. Thanks.

Magento 2

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • Thanks for sharing!

  • jaydip sinh Parmar

    Thanks for Sharing this with details.

    Can this override work for custom module to custom module .
    Actually I tried but failed. There is no error but still my function not call !!
    can you say anything about it ?