Magento Block override rule is similar as helper and model override. Magento follows MVC pattern but Block is the extra layer in Magento. Block class render data for a particular template block. Magento introduces great blocking system in template design system.
Blocks are a way by which Magento distinguishes the array of functionalities in the system and creates a modular way to manage it from both visual and functional stand point. There are two types of blocks and they work together to create the visual output.
* Structural Blocks
These are blocks created for the sole purpose of assigning visual structure to a store page such as header, left column, main column and footer.
* Content Blocks
These are blocks that produce the actual content inside each structural block. They are representations of each feature functionality in a page and employs template files to generate (X)HTML to be inserted into its parent structural block. Category list, mini cart, product tags and product listing…etc are each its own content block.
Instead of including template after template as a typical eCommerce application would in order to gather the whole (X)HTML output, Magento gathers and arranges page content through blocks.
How to override a block ?:
Here i used Customer module as example. And here i describe various customer block class override system as example.
a) Customer register form block (Mage_Customer_Block_Form_Register)
b) Customer view form block (Mage_Customer_Block_Form_View)
c) Customer address edit block (Mage_Customer_Block_Address_Edit)
d) Customer account dashboard info block (Mage_Customer_Block_Account_Dashboard_Info)
e) Customer account dashboard address block (Mage_Customer_Block_Account_Dashboard_Address)
f) Customer widget name block (Mage_Customer_Block_Widget_Name)
Lets start step by step…………
1. At first create new module to process override work and write below code in app/etc/modules/M4U_Customer.xml
<?xml version="1.0"?>
<config>
<modules>
<M4U_Customer>
<active>true</active>
<codePool>local</codePool>
</M4U_Customer>
</modules>
</config>
2. Configure in app/code/local/M4U/Customer/etc/config.xml. Here, I mention 5/6 block override syntax as example. Keep only those classes whose classes you want to override.
<?xml version="1.0"?>
<config>
<modules>
<M4U_Customer>
<version>0.1.0</version>
</M4U_Customer>
</modules>
<global>
<blocks>
<customer>
<rewrite>
<form_register>M4U_Customer_Block_Form_Register</form_register>
<form_view>M4U_Customer_Block_Form_View</form_view>
<address_edit>M4U_Customer_Block_Address_Edit</address_edit>
<account_dashboard_address>M4U_Customer_Block_Account_Dashboard_Address</account_dashboard_address>
<account_dashboard_info>M4U_Customer_Block_Account_Dashboard_Info</account_dashboard_info>
<widget_addressname>M4U_Customer_Block_Widget_Name</widget_addressname>
</rewrite>
</customer>
</blocks>
</global>
</config>
3. Now write your new block class and define all override methods
a) Customer register form block override
class M4U_Customer_Block_Form_Register extends Mage_Customer_Block_Form_Register
{
// override existing method
//write new function
public function newmethod()
{
return true;
}
}
b) Customer view form block (Mage_Customer_Block_Form_View)
class M4U_Customer_Block_Form_View extends Mage_Customer_Block_Account_Dashboard
{
// override existing method
//write new function
public function newmethod()
{
return true;
}
}
c) Customer address edit block (Mage_Customer_Block_Address_Edit)
class M4U_Customer_Block_Address_Edit extends Mage_Customer_Block_Address_Edit
{
// override existing method
public function getAddressStatus()
{
return $this->getRequest()->getParam('address');
}
//write new function
public function newmethod()
{
return true;
}
}
d) Customer account dashboard info block (Mage_Customer_Block_Account_Dashboard_Info)
class M4U_Customer_Block_Account_Dashboard_Info extends Mage_Customer_Block_Account_Dashboard_Info
{
// override existing method
//write new function
public function newmethod()
{
return true;
}
}
e) Customer account dashboard address block (Mage_Customer_Block_Account_Dashboard_Address)
class M4U_Customer_Block_Account_Dashboard_Address extends Mage_Customer_Block_Account_Dashboard_Address
{
// override existing method
//write new function
public function newmethod()
{
return true;
}
}
f) Customer widget name block (Mage_Customer_Block_Widget_Name)
class M4U_Customer_Block_Widget_Name extends Mage_Customer_Block_Widget_Abstract
{
// override existing method
public function showMiddlename()
{
return $this->_showConfig('middlename_show');
}
//write new function
public function newmethod()
{
return true;
}
}