Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between remove and unsetChild methods in layout?

Tags:

What is the difference between remove and unsetChild methods in layout?

For example (in poll.xml layout file):

<customer_account_index>     <reference name="right">         <action method="unsetChild"><name>catalog_compare_sidebar</name></action>     </reference> </customer_account_index> 

Why unsetChild and not just remove?

like image 710
Snowcore Avatar asked Jun 15 '11 12:06

Snowcore


2 Answers

Remove nodes will be processed after all layout handles are merged, and are a good way to remove a block regardless of which layout handle loaded the block; you just want to get rid of it entirely for some handles! It also removes recursively, so all you need to specify is the layout handle.

On the other hand, you may only want to remove a block from a reference in a specific layout handle, in which case you should use unsetChild. It is often used to remove a block from a reference, but then re-insert the same block with a different position. This would not have been possible with remove.

In your very specific example, the magento developers used it to give magento some flexibility. Let's say I added a subpage for the account index page, and the following layout handles were loaded:

  • default
  • ...
  • customer_account_index
  • customer_account_index_subpage

And now assume on this subpage I would actually want the 'catalog_compare_sidebar' block. If they had used 'remove', I would not be able to add this block (with this specific name) because 'remove' would be processed -after- I had added the block myself.

This allows you to easily make changes from one single file; local.xml.

like image 129
Daniel Sloof Avatar answered Oct 20 '22 20:10

Daniel Sloof


Taking your code as an example,if you use unsetchild,compare sidebar block removed from right column,but you can use it anywhere like left column,footer etc..,remove removes entirely from the template,and cannot be used anywhere.

Remove the compare sidebar using remove and if you call it somewhere else,error will be thrown.

like image 38
blakcaps Avatar answered Oct 20 '22 20:10

blakcaps