Codegento Who Let Mage Out Of The Cage?


Rewriting a Block Class

As stated in the blog post about rewriting models, the ability to rewrite classes are a super valuable tool when you want to extend the business logic of Magento, or make tweaks to the core.

Again, I will state that you SHOULD NEVER CHANGE THE CORE, and if you can help it, you shouldn't just copy whole classes down into the local pool. If you want a more extensive lesson on rewriting, read the Rewriting a Model Class post. Below we will show you what you need to do to rewrite a block.

Let's say that you LOVE the feature that allows friends to send their other friends emails about products (does anyone do this?). You love it soooo much that you never want anyone to be stopped from doing so. You notice in the block Mage_Sendfriend_Block_Send the function...

     * Check if user is allowed to send
     * @return boolean
    public function canSend()
        return !$this->_getSendfriendModel()->isExceedLimit();

...and you think to yourself, "There should be NO limit to sending emails!". Here is a time where you would rewrite the block. You have your own "Send" block that you want to use everywhere the Mage_Sendfriend_Block_Send is. Your "Send" block is called Super_Awesome_Block_Example_Send. A simple addition to the config.xml (preferrably in the Super_Awesome module and you are done!


Don't forget to clear the cache!

Now when your layout files say <block type="sendfriend/send" />, or you create a block using $this->getLayout()->createBlock('sendfriend/send', 'sendy'), your Magento application will use the Super_Awesome_Model_Example_Send block. WOOT!

Posted by Ben Robie

Comments (2) Trackbacks (0)
  1. this is more than 1 year ago but, if you can give som help.

    your example works perfect, now how can I add a custom block to an overrided module, let say I want to use “sendmore”, this is not a rewrite, since this is not a block in the core, I know I have to create the ” Super_Awesome_Block_Example_Sendmore” class and everything, but where and how??

    any help ‘ll be really appreciated.

Leave a comment


Trackbacks are disabled.