1

How to remove the left sidebar based on some condition written in block? I have to use the same layout xml file. Based on a condition, need to remove a block through coding. section need to be removed:

 <reference name="left"> <block type="catalog/layer_view" name="catalog.leftnav" before="-" template="catalog/layer/view.phtml"/> </reference> 

I tried with below code.

Mage::app()->getLayout()->unsetBlock('left.catalog.leftnav'); 

I didn't get any error but left sidebar still there. can anyone help me? Thanks.

  1. I cant access controller. If i have access for controller, then i can easily do in layout rendering section. So i added my work as module and created block. Now I need to change layout / remove left sidebar.

  2. Or is It possible to change the layout xml from block? Because i have condition statement in block. so i can choose the layout based on the condition.

Can anyone help me to fix this? Thanks

6
  • Try with Mage::app()->getLayout()->unsetBlock('left'); Commented Dec 30, 2013 at 7:17
  • @Su123 Tried but no error and also block still exist. Commented Dec 30, 2013 at 7:20
  • <remove name='catalog.leftnav'/> Commented Dec 30, 2013 at 8:20
  • Mage::app()->getLayout()->getUpdate()->addUpdate("<remove name=\"left\"/>"); Commented Dec 30, 2013 at 8:40
  • @Su123 I have placed your code in block function. Still it not working. Can you tell me where should i add? Commented Dec 30, 2013 at 12:27

2 Answers 2

3

If you do not want to use an observer what you could do is rewrite the block Mage_Catalog_Block_Layer_View and override the function _toHtml so that it performs the conditional check you want.

protected function _toHtml() { if (your conditional check) { return ''; } return parent::_toHtml(); } 
2

you can try to listen some event like controller_action_layout_load_before and you will get layout object like

$update = $observer->getEvent()->getLayout()->getUpdate();

now you can remove/unset the block

4
  • I want to do in block not in controller. is there anyway? Commented Dec 30, 2013 at 7:32
  • 1
    This is not in controller.Its an observer event Commented Dec 30, 2013 at 7:55
  • I'm new magento. I dont know where to implement your code. I can't access the controller. So i'm just doing in block and calling block in layout. Can you explain more about your answer. how to add event listener etc.., Commented Dec 31, 2013 at 5:00
  • Can't you do it using xml ? Commented May 5, 2014 at 6:47

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.