Overriding Frontend / Adminhtml blocks - Magento Development

Today i’ve been trying to override adminhtml/page_menu block and the corresponding template file page/menu.phtml. I need to update a function getMenuLevel(), which basically generates the html for the menu in admin panel. Needless to say, with magento’s awesome flexibility it turns out to be a piece of cake (although i did spend an hour on a stupid typo).

app/code/local/Market/Adminhtml/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <modules>
    <Market_Adminhtml>
      <version>0.1.0</version>
    </Market_Adminhtml>
  </modules>

  <!-- Global -->
  <global>
    <blocks>
      <market_adminhtml>
        <class>Market_Adminhtml_Block</class>
      </market_adminhtml>
      <adminhtml>
        <rewrite><page_menu>Market_Adminhtml_Block_Page_Menu</page_menu></rewrite>
      </adminhtml>
    </blocks>
  </global>
</config>

blocks node tell magento about the blocks in our module. market_adminhtml is the unique identifier to the blocks we’re gonna include in our module. market_adminhtml>class defines the class prefix for our blocks. Since we’re not going to create new blocks, those nodes are not important.

adminhtml node is the unique id to the blocks defined in app/code/core/Mage/Adminhtml/Block/, you can verify the same in config.xml for the Mage/Adminhtml module. Inside the rewrite node we suggest magento to use Market_Adminhtml_Block_Page_Menu class whenever a block of type adminhtml/page_menu is requested.

app/code/local/Market/Adminhtml/Block/Page/Menu.php

class Market_Adminhtml_Block_Page_Menu extends Mage_Adminhtml_Block_Page_Menu{

  public function foo(){
    return "<h1>foo</h1>";
  }

  public function getMenuLevel($menu, $level = 0)
  {
    $html = "";
    .
    .
    .
    return $html;
  }      
}

The only thing you gotta remember is to extend the block we’re going to override (Mage_Adminhtml_Block_Page_Menu here), so you can keep the existing block functionalities. As you can figure out, i’ve overridden only getMenuLevel() function, for the rest of the functions magento still refers to the overridden class.

That’s all. The procedure is similar for frontend blocks too. Hope this helps.

Beware of typos

I closely spend an hour on a typo in my config.xml. Market_AdminHtml_Block_Page_Menu (with a capital ‘h’ in ‘Adminhtml’) instead of Market_Adminhtml_Block_Page_Menu. Kinda hard to recognize, but it’ll still confuse magento. Hope you don’t make such silly mistakes.

 
0
Kudos
 
0
Kudos