How to override a Magento core block class

By April 9, 2012 No Comments

Some discussion on breadcrumbs: I will be overriding the Core file: app/code/core/Mage/Catalog/Block/Breadcrumbs.php (which I will call [Breadcrumbs-B]). This file is not to be confused with the block: app/code/core/Mage/Page/Block/Html/Breadcrumbs.php(which I will call [Breadcrumbs-A]).

[Breadcrumbs-A] contains the notable function ‘addCrumb‘ which adds a crumb to the line of breadcrumbs (to a breadcrumbs array). It can be used many times throughout the site. This file also explicitly uses the page/html/breadcrumbs.phtml file via this line of code in the constructor function: $this->setTemplate(’page/html/breadcrumbs.phtml’);

This block is called from the page.xml layout file (which does not set a template, as this block code sets it’s own template as I just mentioned).

[Breadcrumbs-B] uses [Breadcrumbs-A] via this line of code: $breadcrumbsBlock = $this->getLayout()->getBlock(’breadcrumbs’)

The $breadcrumbs->addCrumb() method is then used to create the crumbs within the block’s _prepareLayout() method.

Confused? Probably. It’s a slightly circular way of using Block code logic and template files.

Anyway, on to the main point of this tutorial: Overriding the breadcrumbs.php block [Breadcrumbs-B]. Why choose this one? Because it controls the layout portion of the breadcrumbs (in conjunction
with the corresponding .phtml file, of course).

The easiest way to override a piece of core code is to simply add it and it’s folder structure to the local folder. We could easily override app/code/core/Mage/Catalog/Block/Breadcrumbs.php by putting it here: app/code/local/Mage/Catalog/Block/Breadcrumbs.php

However, we may want to override it in the true Magento fashion in order to keep our changes truly modular (which of course makes sharing our modules that much easier!)

You may want to do the “easier” method if multiple modules require modification to a Core file, however (to reduce / eliminate conflicts in module code). This tutorial will show you the true override method so we can keep our modules…modular.

So, we will  be creating 3 files (2, if you created my other custom module from my last custom module post and used the same file names)

  • app/code/local/Fido/Catalog/Block/Breadcrumbs.php
  • app/code/local/Fido/Catalog/etc/config.xml
  • app/etc/modules/Fido_All.xml

Step 1

Tell Magento about your new module (app/etc/modules/Fido_All.xml):

<?xml version="1.0"?>

Step 2

Your config.xml file (app/code/local/Fido/Catalog/etc/config.xml )

<?xml version="1.0"?>

Notice the tag. Also notice the is on the on line as the name of the class (because of a bug in Magento, there can be no whitespace or return there).

Step 3

Override the block (app/code/local/Fido/Catalog/Block/Breadcrumbs.php)

class Fido_Catalog_Block_Breadcrumbs extends Mage_Catalog_Block_Breadcrumbs  #note: extending file I'm overwriting, not the abstract class the original class extends (is this correct?)
    protected function _prepareLayout()
        if ($breadcrumbsBlock = $this->getLayout()->getBlock('breadcrumbs')) {
          array('label'=>Mage::helper('catalog')->__('Home'),'title'=>Mage::helper('catalog')->__('Go to Home Page'), 'link'=>Mage::getBaseUrl())
            //comment the above code out to remove the "home" link from your breadcrumbs.
            $title = (string)Mage::getStoreConfig('system/store/name');
            $path = Mage::helper('catalog')->getBreadcrumbPath($this->getCategory());
            foreach ($path as $name=>$breadcrumb) {
            $breadcrumbsBlock->addCrumb($name, $breadcrumb);
            $title = $breadcrumb['label'].' '.Mage::getStoreConfig('catalog/seo/title_separator').' '.$title;
        if ($headBlock = $this->getLayout()->getBlock('head')) {
    echo 'yay'; /*The new code just to test if it works! (string appears at the top of the screen if it is) - this should not be left in the code! It's just a method I use to see if Magento is reading the block class at any point.*/
    return parent::_prepareLayout();

I made some comments in the above code so you can see some possible edits.
Another tip: If you are wondering just what file Mage::helper(’catalog’) relates to as I was wondering (there is no ‘catalog.php’ helper file amongst the helpers in the core catalog module) you can add this code in your block to test:
echo get_class(Mage::helper('catalog'));
//outputs Mage_Catalog_Helper_Data

It appears that data.php is a default helper file that Magento searches for. (You would usually use something like Mage::helper(’catalog/data’); )

Source : http://www.exploremagento.com