How to override a Magento core block class

Posted on 17. Sep, 2008 by Fido in Design, Development, Magento, Module

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

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"?>
<config>
    <modules>
        <Fido_Catalog>
            <active>true</active>
           <codePool>local</codePool>
       </Fido_Catalog>
    </modules>
</config>

Step 2

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


<?xml version="1.0"?>
<config>
    <global>
        <blocks>
            <catalog>
                <rewrite>
                    <breadcrumbs>Fido_Catalog_Block_Breadcrumbs</breadcrumbs>
                </rewrite>
            </catalog>
        </blocks>
    </global>
</config>

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')) {
/*$breadcrumbsBlock->addCrumb('home',
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')) {
$headBlock->setTitle($title);
}
}
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’); )

Tags: , , , , ,

13 Comments

Skeuds

30. Sep, 2008

Hi,

Thank you for this great tutorial, i don’t have the result that i have hope, the breadcrumbs now print for example : Book / Stephen king / Home(the link home appears at the end of the breadcrumbs). Have you any idea to correct it ?

Thank you.

Fido

01. Oct, 2008

What modification did you do exactly? You might want to email me to show me your code (contact section).
What Magento version are you using?

Audrey

01. Oct, 2008

Hi, it works fine for Breadcrumbs, but do you have an idea about navigation ?
I’d like to change the top navigation. I have a lot of subcategories, so it makes a very long list and very long page without anything at the bottom.

Thank you !

Fido

01. Oct, 2008

How do you want to change the top navigation? The files are easy enough to find - there’s a mix of javascript and HTML output in there that complicates things a little (but not too too badly)

Audrey

02. Oct, 2008

For example,
I have 3 categories Books / Press / Publications.
For Books, I have 63 sub categories… So the list in the menu is too long and very easy to use, I have to scroll the page to see the sub categories from nearly 20th sub categorie till the end of the list.

I would like to make like 3 columns with sub categories.

I think I only need to change the function witch create the tree, so I need to rewrite Mage_Catalog_Block_Navigation.

What do you think ?

Fido

02. Oct, 2008

Check out this blog post: http://www.exploremagento.com/magento/editing-the-navigation.php

To see some files that contain code you could use to edit the navigation.

Dinesh

14. Oct, 2008

Hello

How can i call the function of other class of other module from the custom module.

Thanks

Fido

14. Oct, 2008

That depends. You can grab helpers from other modules easily using $this->helper(’module/helperFile’)

Example:
$this->helper(’catalog/product’)->getEmailToFriendUrl($_product);

The getEmailToFriendUrl is defined in the Product.php file in the Catalog module Helper folder.

You can get Models in this way:
$category = Mage::getModel(’catalog/category’)->load($this->getCategoryId());

This code can be used anywhere. This particular code will return a product category object

This is grabbing Category.php from app\code\core\Mage\Catalog\Model

Other useful code snippets to get you on your way:
$layer = Mage::getSingleton(’catalog/layer’);

$categories = Mage::registry(’product’)->getCategoryCollection()
->setPage(1, 1)
->load();

Hope this helps!

[...] and create your own template for over-riding core modules with your custom code, creating your own custom modules and your own template files. Also get jQuery working properly if [...]

JD

27. Jan, 2009

Hi, thanks for the tutorial. The hardest part of this is finding the actual block to override. There seem to be many orphaned bits of code and even with the block hints turned on, setting the rewrite path in the config.xml is a challenge. Any suggestions for tracking that down?

Heikki

10. Feb, 2009

What is the difference between

Mage::getModel

and

Mage::getSingleton
?

[...] Override a Magento Core Module - Magento Custom Module - Magento Development | Explore MagentoThe proper method of over-riding a magento core block. [...]

James Kim

14. Feb, 2009

Great post!
Question. How would you override controller? Or is it possible at all?

Thanks,
James

Leave a reply