Thursday, March 22, 2012

Remove Head Block From Page


You can either unset head block entirely, or just remove the script files. What you need to do is to create an observer on

controller_action_layout_render_before
event, and create this observer function:
public function removeHead()
{
 if (Mage::app()->getRequest()->getModuleName() == "lazyload") { //put your module/controller/action name here so you don't remove head for every single page
  $layout = Mage::getSingleton('core/layout');
  $layout->unsetBlock('head');

  /* second level of destruction - because this block has two lives .. like a final queen in an arcade */

  $head = $layout->getBlock('root')->getChild('head');
  if (is_object($head)) {
   $head->setData('items',array());
   $layout->getBlock('root')->setChild('head',$head);
  }

  /* third level of destruction - if the bitch respawns */
  //$layout->getBlock('root')->setChild('head', new Mage_Core_Block_Template);
 }
}

4 comments:

  1. Dear Bhoopendra,

    I am a beginner in Magento.

    Though I am yet to test the above codes, it appears to be an excellent work.

    Keep it up!

    Colonel SC Sood (Retd), Professor
    satishsood1@gmail.com
    Mobile: 9216508708

    ReplyDelete
  2. Honestly I am a newbie also about magento that's why I am very glad that I came across with your post. I find a lot of worthy points here with regards to magento. Big thanks.

    ReplyDelete
  3. I have learnt that to remove a block from a page you can simply update that page's xml handle in layout/module.xml. The handle is based on the module's route, the controller and the action, hence in your app/design/frontend/package/theme/layout/{module}.xml (assuming {module} is the name of the module:

    < layout version="0.1.0">
    ....
    < module}_{controller}_{action}>< !-- start handle -->
    < remove name="head"> < !-- this removes the head layout element, ONLY on the page referred to by this handle -->
    < /{module}_{controller}_{action}>< !-- endhandle -->
    < /layout>

    ReplyDelete