How to Remove and Add Block in Magento Layout

blockslayout-updatetheme

so I came across a question which I don't understand it properly

    <default>
    <reference name="header">
    <remove name="top.menu"/>
    <block type="core/text_list" name="top.menu" as="topMenu">
    <lable>Navigation Bar</label>
    <block type="page/html_topmenu" name="catalog.topnav" template="page/html/topmenu.phtml"/>
</block>
</reference>
</default>

in my understanding we have a header block somewhere else and we referenced to it,then we remove the block of top.menu within this block
then we add a structural block of top.menu and then we add other block within this block which is a template of top menu
so I don't really know what's happening does the top.menu has been deleted? or it isn't and it has been added again?
can someone explain it to me in a way I can understand blocks better?

the answer was the block will never be displayed! and It seems I was right and it is not the correct answer

Is there any way that we delete the block and after that we add the same block so that in the end we didn't change anything? for example I have block A I remove it once but again I add block A again and still I can see it in the page

Thanks

Best Answer

So in short, all of your assumptions are absolutely right. To make it more clear, I will try to explain it in details

  1. So here the layout handle used is default. This means, the layout updation that is hold by this handle will apply for almost all pages.

  2. You are referencing to header block then. This block is defined in page.xml. Referencing this block is very important. You should "refer" to a block only after it is defined somewhere before it get referenced in your update. So here, you can refer this block only when if your layout update file is get considered by magento after page.xml file get processed. I assume you are doing it in the right way :-)

  3. Now your update script is removing the block topMenu. That simple line do lot of jobs and that is what you probably didn't get. Suppose before processing your layout update, there are 10 top menus are added via another layout update XML Files (It may be programmatically also). So when your layout update is processed by magento, all those menu will deleted.

In short, removing a block removes that block along with all of its child block and all properties. So this line simply removes the entire top menu part along with all changes that have applied to this block through all other processed layout XML Files (if any)

  1. Now it again add a new topmenu block and then set a template for that top menu. This means you have again created a "fresh" top menu block which does not contain any child blocks or menus by default.

In short, your layout update script tries to remove all topmenu items that are previously created via other layout XML Files that had been processed by Magento before this layout update file get processed. Then it defines a fresh top menu items that may use to add top menus later.

EDIT

I think you are still not clear with my point. OK I will explain it with a real time situation.

Suppose you need 4 pizzas to deliver. These 4 pizzas are made up of different ingredients (some ingredients are same and some are different). After prepared them, you have found that one of the pizza is not good for deliver. SO you removed that pizza and created another pizza instead for that. Then you delvivered those 4 pizzas to your customer.

Here pizza stands for topMenu block. So you have 4 topmenu blocks. Ingredients stands for its child blocks. As in the pizza's case, those blocks will treat as separate entities by Magento. Then you have removed the unwanted block then replaced it with another one. This exactly like in the pizza case. Removed and added pizzas are different and the only existing similarity is, they are of same type. Still at the end there are 4 pizzas available. So we have a topmenu available. Did you get now ?

Related Topic