How To Rewrite Core Block – Rewritting Footer Block

Prerequisities

Start again creating clear new module. Than turn developer’s mode on.

Which block to override

I decided that I want to replace core footer with image from my favorite movie. When you see open your page, you will see that footer’s block is Mage_Page_Block_Html_Footer.

Footer

Create the block

We will create our own Block now. Important thing is that our Block has to extends Mage_Page_Block_Html_Footer – the one we are going to override. It has to be placed in same directory and has the same name.

When you know the Block name, it’s very easy to find out where the block is located.

footer file structure

Which means that we need to create in our module’s file /Block/Html/Footer.php

For now we will copy all original Footer.php file from core, but with one change. We have to change Block class name. Final file will look like this

<?php
echo "test, Im here in rewritten footer";
class Lukas_Footer_Block_Html_Footer extends Mage_Page_Block_Html_Footer // we are extending from original class name.
{

// This code down is from original file.
  protected $_copyright;

  protected function _construct()
  {
    $this->addData(array('cache_lifetime' => false));
    $this->addCacheTag(array(
      Mage_Core_Model_Store::CACHE_TAG,
      Mage_Cms_Model_Block::CACHE_TAG
    ));
  }

  /**
   * Get cache key informative items
   *
   * @return array
   */
  public function getCacheKeyInfo()
  {
    return array(
      'PAGE_FOOTER',
      Mage::app()->getStore()->getId(),
      (int)Mage::app()->getStore()->isCurrentlySecure(),
      Mage::getDesign()->getPackageName(),
      Mage::getDesign()->getTheme('template'),
      Mage::getSingleton('customer/session')->isLoggedIn()
    );
  }

  public function setCopyright($copyright)
  {
    $this->_copyright = $copyright;
    return $this;
  }

  public function getCopyright()
  {
    if (!$this->_copyright) {
      $this->_copyright = Mage::getStoreConfig('design/footer/copyright');
    }

    return $this->_copyright;
  }

  /**
   * Retrieve child block HTML, sorted by default
   *
   * @param   string $name
   * @param   boolean $useCache
   * @return  string
   */
  public function getChildHtml($name='', $useCache=true, $sorted=true)
  {
    return parent::getChildHtml($name, $useCache, $sorted);
  }

}

Replace old block with new one

It’s nice that we have created new block, but we need tell Magento that we want to load this block instead of the core one. We can do that in our module’s /etc/config.xml which will ends up looking like this

<config>
    <modules>
        <Lukas_Footer>
            <version>1.0.0</version>
        </Lukas_Footer>
    </modules>
    <global>
        <blocks>
            <page> <!--I got this tag from the original module's config.xml file-->
                <rewrite>
                    <html_footer>Lukas_Footer_Block_Html_Footer</html_footer> <!--Last two segments of our block class-->
                </rewrite>
            </page>
        </blocks>
    </global>
</config>

Now when you refresh page you will see, that source of footer is now Lukas_Footer_Block_Html_Footer.


Lukas
Lukas Stranovsky
I am the guy who made this. Sorry for typos.