Overriding Magento2 blocks,models and controllers

Magento2 overriding

In module development, Overriding the core functionality is an important process. We will face it frequently when modifying core functionalities.The main reason of overriding is, the website developer should never do any modifications in core files for their customization. Incase if they need to do, they can simply extend the core class by using rewrite tag in config file of external module, the overall process is called magento overriding. To know more about overriding click here. In this article Overriding Magento2 was described from Magento 1.x for your clear understandings. The syntax of rewrite in magento2 was completely differed from magento1.

Overriding Magento 1.x

In magento 1.x the overriding of blocks and models are almost same. You have to initiate the rewrite class in the config file.

overriding blocks

	<config>
	<global>
		<blocks>
			<catalog>
				<rewrite>
					<product_list>Explorer_Test_Block_Product_View</product_list>
				</rewrite>
			</catalog>
		</blocks>
	</global>
	</config>
	

The View.php file in Explorer/Test/Block/Product

	class Explorer_Test_Block_Product_View extends Mage_Catalog_Block_Product_View
	{
		// some code
	}
        

overriding models

	<config>
	<global>
		<models>
			<catalog>
				<rewrite>
					<product>Explorer_Test_Model_Product</product>
				</rewrite>
			</catalog>
		</models>
	</global>
	</config>
	

The Product.php file in Explorer/Test/Model/

	class Explorer_Test_Model_Product extends Mage_Catalog_Model_Product
	{
		// some code
	}
        

overriding conntroller

	<config>
	<frontend>
		<routers>
			<catalog>
				<args>
					<modules>
						<explorer_test before="Mage_Catalog">Explorer_Test</explorer_test>
					</modules>
				</args>
			</catalog>
		</routers>
	</frontend>
	</config>
	

The ProductController.php file in Explorer/Test/controllers

	require_once(Mage::getModuleDir('controllers','Mage_Catalog').DS.'ProductController.php');
 
	class Explorer_Test_ProductController extends Mage_Catalog_ProductController
	{
	// some code
	}
        

Overriding Magento2

Before moving to Overriding in magento 2. You should know about the complete module development structure in magento 2. To know about full module development click here.
In magento 2.0 all the rewrite rules should be written in di.xml in the etc folder. There is an excellent feature called ‘plugin’ available in magento 2 to overwrite the core functionality. By using plugin in rewrite, we can use ‘before’,’after’,’around’ execution of the core functions. The sample code for rewriting blocks/model/Controllers.

In di.xml in etc folder


	<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Block\Product\View">
        <plugin name="inroduct-custom-module" type="Explorer\Test\Model\Plugin\Product" sortOrder="1"/>
    </type>
    <type name="Magento\Catalog\Model\Product">
        <plugin name="getname-test-module" type="Explorer\Test\Model\Plugin\Product" sortOrder="10"/>
    </type>
    <type name="Magento\Catalog\Controller\Product\View">
        <plugin name="product-cont-test-module" type="Explorer\Test\Model\Plugin\Product" sortOrder="10"/>
    </type>
</config>

The rewrite of model file example

<?php
namespace Explorer\Test\Model\Plugin;

class Product
{        
    protected $_logger;
    public function __construct (
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->_logger = $logger; 
    }

     public function afterGetName(\Magento\Catalog\Model\Product $subject, $result)
    {
        return '|' . $result . '|';
    } 
    public function aroundGetProduct(\Magento\Catalog\Block\Product\View $subject, \Closure $proceed)
    {

        $returnValue = $proceed(); // it get you old function return value
        $name='#'.$returnValue->getName().'#';
        $returnValue->setName($name);
        return $returnValue;// if its object make sure it return same object which you addition data
    }
    public function aroundExecute(\Magento\Catalog\Controller\Product\View $subject, \Closure $proceed)
    {
        $returnValue = $proceed(); // it get you old function return value
        return $returnValue;// if its object make sure it return same object which you addition data
    }
}