Removing XML Blocks in Magento

This article will help you go about removing XML blocks in Magento.

Magento’s default theme comes with default layout blocks included such as a PayPal logo, a community poll and other unnecessary inclusions.

Removing XML Blocks in Magento

More than likely, you will want to remove these blocks when building your own custom theme.

There are two main methods of removing blocks in layout XML.

Let’s try an example of removing the newsletter block that appears in the left sidebar on the home page.

We can either use the unsetChild() action method.

<?xml version="1.0"?>
<layout>
    <cms_index_index>
        <reference name="left">
            <action method="unsetChild">
                <name>left.newsletter</name>
            </action>
        </reference>
    </cms_index_index>
</layout>

Or by using the <remove> node.

<?xml version="1.0"?>
<layout>
    <cms_index_index>
        <remove name="left.newsletter" />
    </cms_index_index>
</layout&gt;

Note that left.newsletter is the name given to the newsletter block by Magento. You can see this block defined within the newsletter.xml layout file.

What are the main differences between the two ways we can remove blocks?

The <remove> option doesn’t need to be defined with a pair of <reference> nodes. This is because it operates in the global block scope of the Mage_Core_Model_Layout class.

It is also used to prevent the block with the specified name from even being instantiated.

So for example if we wanted to remove the newsletter block from all pages by adding the remove node inside the pair of <default> layout handle nodes. Then it was decided that the newsletter block should show on the home page only, the layout XML might look something like this.

<?xml version="1.0"?>
<layout>
    <default>
        <remove name="left.newsletter" />
    </default>

    <cms_index_index>
        <reference name="left">
            <block type="newsletter/subscribe" name="left.newsletter" template="newsletter/subscribe.phtml" />
        </reference>
    </cms_index_index>
</layout>

But you’ll notice that when refreshing the home page, the block does not appear! This is because of the effects that the <remove> node has.

The unsetChild() method on the other hand will allow you to do this as it is a block-level method.

<?xml version="1.0"?>
<layout>
    <default>
        <reference name="left">
            <action method="unsetChild">
                <name>left.newsletter</name>
            </action>
        </reference>
    </default>

    <cms_index_index>
        <reference name="left">
            <block type="newsletter/subscribe" name="left.newsletter" template="newsletter/subscribe.phtml" />
        </reference>
    </cms_index_index>
</layout>

An important point to note with using unsetChild(), is that the method operates with the block alias if it has been defined.

So if Magento modified their newsletter block in the newsletter.xml layout file to include an alias (the as attribute) like the example below.

<block type="newsletter/subscribe" name="left.newsletter" as="leftnewsletter" template="newsletter/subscribe.phtml" />

All of a sudden, we would need to use leftnewsletter rather than left.newsletter when using the unsetChild() method.

<?xml version="1.0"?>
<layout>
    <default>
        <reference name="left">
            <action method="unsetChild">
                <name>leftnewsletter</name>
            </action>
        </reference>
    </default>
</layout>

If an alias has not been defined however, Magento will look for the block name as a fallback.

Note: This article is based on Magento Community/Open Source version 1.9.