How to override a Magento core block class

This tutorial will show you the proper method for overriding a Mage core class. This will work for Block and Model classes. Controllers are a slightly different story and for another tutorial.

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).

download windows 8.1 disc image (ISO File) Windows 8.1 Standard Key
windows 8 is a personal computer operating system developed by microsoft as part of the windows nt family of operating systems windows 8.1 Enterprise Key
development of windows 8 started before the release of its predecessor,windows 7,in was announced at ces 2011,and followed by the release of three pre-release versions from windows 8.1 Professional Key
windows 8(windows),free and safe download Windows 8 Enterprise Key
windows 8 latest version:good for tablets:touchscreen operation,new apps and easier on the battery Windows 8 Professional Key

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)

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

04. <Fido_Catalog>
05. true
06. local

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

07. Fido_Catalog_Block_Breadcrumbs

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)

01.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?)
03.protected function _prepareLayout()
05.if ($breadcrumbsBlock = $this->getLayout()->getBlock(‘breadcrumbs’)) {
07.array(‘label’=>Mage::helper(‘catalog’)->__(‘Home’), ‘title’=>Mage::helper(‘catalog’)->__(‘Go to Home Page’), ‘link’=>Mage::getBaseUrl())
09.//comment the above code out to remove the “home” link from your breadcrumbs.
11.$title = (string)Mage::getStoreConfig(‘system/store/name’);
12.$path = Mage::helper(‘catalog’)->getBreadcrumbPath($this->getCategory());
13.foreach ($path as $name=>$breadcrumb) {
14.$breadcrumbsBlock->addCrumb($name, $breadcrumb);
15.$title = $breadcrumb['label'].’ ‘.Mage::getStoreConfig(‘catalog/seo/title_separator’).’ ‘.$title;
18.if ($headBlock = $this->getLayout()->getBlock(‘head’)) {
22.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.*/
23.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:
1.echo get_class(Mage::helper(‘catalog’));
2.//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’); )

Comments are closed.