Understand Overriding in Magento: Blocks

It's always an important feature for any framework to support extendability and scalability in the core itself. It's so true for Magento, which allows programmers to create custom extensions to fulfill custom requirements, thus providing extendability. On the other hand, it also allows you to override core files to alter the flow and behavior of the core features of Magento.

There are different ways in Magento which allow you to override core files. One of them is the implementation of the Event Observer Pattern. Using that method, you'll define observers in your module for the desired events, and they'll be picked up when associated events are raised in Magento. It's certainly a powerful and standard way to plug in your changes to the core modules of Magento. Having said that, there are situations in which you'll feel that it would great if you could simply replace the core file snippets with your ones. You can do this using an XML configuration file in your custom module.

Why Overriding Blocks?

In Magento, blocks are primitive building components for any layout in the front­-end. Although you can override the template file of any block using a custom theme to change the visual output, sometimes you would like to alter the logic of the core block code. In that case, overriding a core block with your custom module's block file really helps to plug in your changes easily.

In this tutorial, we'll make a custom module to understand how block overriding works in Magento. In the custom module, we'll override a product listing block displayed on the category page view. Overriding works by setting up certain XML tags as per the conventions of Magento.

I assume that you're familiar with the basic module creation process in Magento. Here's a nice article explaining the basics of custom module creation, just in case you would like to have a look at it! Let's jump straight away into the content of this tutorial!

A Glance at a Setup

We'll create a simple custom module named "Catalog", which sets up overriding of the core product listing block (app/code/core/Mage/Catalog/Block/Category/View.php) in the category page view. Here's a list of the files required for the desired setup:

  • app/etc/modules/Envato_All.xml: It's a file used to enable our custom module.
  • app/code/local/Envato/Catalog/etc/config.xml: It's a module configuration file in which we'll set up block overriding using certain tags as per the Magento conventions.
  • app/code/local/Envato/Catalog/Block/Category/View.php: It's our new block file which will replace a core block file.

Creating Files and Folders: Custom Module

First, we need to create a module enabler file. Create a file "app/etc/modules/Envato_All.xml" and paste the following contents in that file. We've used Envato as our module namespace and Catalog as our module name. It'll enable our Catalog module by default.

Next, we need to create a module configuration file. Create "app/code/local/Envato/Catalog/etc/config.xml" and paste the following contents in that file.

In the beginning of the module, we've set up the module version number using the <version> tag. After that, the <catalog> and <rewrite> tags are used to inform the Magento overriding system that we're going to override one of the "blocks" of the "Catalog" core module.

Further, the <category_view> tag is used to define a block identity which will be overridden by the Envato_Catalog_Block_Category_View class. It's mapped to a block file "Category/View.php" under the "Block" directory of the Catalog module. The important thing to notice here is that we're following a directory structure similar to the core module. Although that's not strictly necessary, it's preferred over the different directory structure to maintain readability.

Finally, the only remaining thing is to define a block class Envato_Catalog_Block_Category_View. Let's create a block file "app/code/local/Envato/Catalog/Block/Category/View.php" and fill it up using the following contents.

We've defined the Envato_Catalog_Block_Category_View class which extends the core Mage_Catalog_Block_Category_View block class. Thus, you can override every method of the base class and create new methods if necessary.

In the above example, the getProductListHtml method is overridden so it'll be called on the category page view! You can alter the code as per your requirements.

Although it's a very basic example, it serves the purpose of overriding the core block, and you can tweak it to make things more complex and create something more useful!

Conclusion

Today, in the first article of this series, we've learned how to override the core blocks of Magento. In the next part, I'll come up with more exciting stuff! I would love to hear your comments and queries in the feed below!

Tags:

Comments

Related Articles