Magento 2: Override/Rewrite Block, Model, Controller, Helper

In most cases, when work with Magento, you will have to change the core functionality of block, model, controller and helper. It is not good if you modify the core files, that may have certain influence on another program. Therefore, today, I highly recommend the great way to rewrite all files in a convenient way.

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

There are two ways to override / rewrite Block, Controller, Model, and Helper in Magento.

  1. Using Plugin :

    A plugin allows you to execute code before, around, or after methods from the class you’re hooking onto. Your plugin class does not replace the target class, and it is not an instance of it. You just have methods before{method}, around{method}, after{method} which get executed at the appropriate time in respect to {method} on the target class.

    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.

    Since plugins do not replace the target class, any number of plugins can be active on a class simultaneously. Magento just executes them one after another based on the sortOrder parameter in your XML.

    Because of that, Plugin appears as the clever choice to rewrite block, model, controller, helper in Magento 2.

  2. Using Preference : 

    A preference is equivalent to class rewriting from Magento 1. It’s equivalent to saying, “Whenever code asks for ClassA, give them MyClassB instead.” MyClassB is expected to be a complete implementation of ClassA, plus whatever behavior you add or modify on top.

    As in Magento 1, only one preference (rewrite) can be active for a class at one time unless you chain them manually (such that MyClassB extends OtherClassB, and OtherClassB extends ClassA).

    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.

    Let’s see, how we can override / rewrite Block, Controller, Model and Helper using plugin and preference in Magento 2.

To do this, I am going to create two modules in Magento.

  1. DemoOverridePlugin – This module is to test the Plugin method.
  2. DemoOverridePreference – This module is to test the Preference method.

Here is the folder structure for my two modules.

 

  

  

  

  
  

  

  

  
 





If you want to learn more about creating custom module in Magento2, Please follow this link – http://blog.iyngaran.info/creating-running-cron-in-magento-2-custom-module.

Method 1: 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 Iyngaran_DemoOverridePlugin.

Block Override

app/code/Iyngaran/DemoOverridePlugin/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/Iyngaran/DemoOverridePlugin/Plugin/ProductPluginView.php

That’s it!.

Clear cache and reload the product page.

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

Model Override

app/code/Iyngaran/DemoOverridePlugin/etc/di.xml

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

app/code/Iyngaran/DemoOverridePlugin/Plugin/ProductPluginModel.php

That’s it!.

Clear cache and reload the product page.

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

Controller Override



app/code/Iyngaran/DemoOverridePlugin/etc/di.xml

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

app/code/Iyngaran/DemoOverridePlugin/Plugin/ProductPluginController.php

That’s it!.

Clear cache and reload the product page.

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

Helper Override

app/code/Iyngaran/DemoOverridePlugin/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/Iyngaran/DemoOverridePlugin/Plugin/ProductPluginHelper.php

That’s it!.

Clear cache and reload the product page.

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

Here is the complete source code for DemoOverridePlugin – https://github.com/iyngaran/magento/tree/master/Override_Rewrite_Block_Model_Controller_Helper_using_plugin/app/code/Iyngaran/DemoOverridePlugin

Method 2: Using Preference

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

We have to define preference in in app/code/Iyngaran/DemoOverridePreference/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/Iyngaran/DemoOverridePreference/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/Iyngaran/DemoOverridePreference/Block/Catalog/Product/View.php

That’s it!.

Clear cache and reload the product page.

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

Model Override

app/code/Iyngaran/DemoOverridePreference/etc/di.xml

Let’s rewrite getName() function of class Magento\Catalog\Model\Product and will add ‘Hello – ’ to the product name. So we will be able to see the ‘Hello – ’ text in front of the product name in product page. And also we will just log some message on var/log/debug.log for this test.

app/code/Iyngaran/DemoOverridePreference/Model/Catalog/Product.php

That’s it!.

Clear cache and reload the product page.

Look at the product name in product page and category page, all products will have ‘Hello – ’ in front of the name.

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

Controller Override

app/code/Iyngaran/DemoOverridePreference/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/Iyngaran/DemoOverridePreference/Controller/Product/View.php

That’s it!.

Clear cache and reload the product page.

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

Helper Override

app/code/Iyngaran/DemoOverridePreference/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/Iyngaran/DemoOverridePreference/Helper/Catalog/Data.php

That’s it!.

Clear cache and reload the product page.

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

Here is the complete source code for DemoOverridePreference – https://github.com/iyngaran/magento/tree/master/Override_Rewrite_Block_Model_Controller_Helper_using_preference/app/code/Iyngaran/DemoOverridePreference

Hope this helps.

2 Comments

Leave a Reply

Your email address will not be published.


*


*