Fixing Invalid Block Type Errors in Magento

One of the reasons why Magento’s system and exception log files get filled so quickly is due to repeated errors being generated when the Magento application loads on each page request.

One of the most common errors seen is the 'Mage_Core_Exception' with message 'Invalid block type: [block] error. This error occurs when Magento tries to load a block that doesn’t exist from within a layout file.

There are a couple of ways we can investigate and correct this error.

Check local.xml Layout Updates

When a third party extension has been installed, a developer may look to make layout updates to this extension, perhaps in the theme’s local.xml layout file.

As an example, let’s imagine that an extension has been added to add a jQuery slider plugin to the home page. The layout XML might look like the following.

// app/design/frontend/base/default/layout/custom_homesliders.xml

<?xml version="1.0"?>
<layout>
    <cms_index_index>
        <reference name="content">
            <block type="custom/homesliders" name="custom.homesliders.sliders" as="custom_sliders" template="homesliders/sliders.phtml" />
        </reference>
    </cms_index_index>
</layout>

If for example, the extension was modified to include the sliders on category pages, layout updates within the local.xml file might look like this.

// app/design/frontend/[package_name]/[theme_name]/layout/local.xml

<?xml version="1.0"?>
<layout>
    <catalog_category_view>
        <reference name="content">
            <block type="custom/homesliders" name="custom.homesliders.sliders" as="custom_sliders" template="homesliders/sliders.phtml" />
        </reference>
    </catalog_category_view>
</layout>

If the third party extension is ever disabled, Magento will still try to render any layout updates from within local.xml. The custom/homesliders block is therefore not found, and the error is generated in the log file upon every category page request.

To fix this issue, you can either move the layout updates within the extension’s layout XML file. This isn’t the most recommended method, as if an updated version of the extension is installed on your site, any layout updates made will be lost.

Create an Override Module

It is far better to add your own custom module that is dependent on the jQuery slider module. You can then declare your own custom layout file and include the update.

Add your module including the slider module dependency.

// app/etc/modules/Homesliders_Override.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Homesliders_Override>
            <codePool>local</codePool>
            <active>true</active>
            <depends>
                <Custom_Homesliders />
            </depends>
        </Homesliders_Override>
    </modules>
</config>

Declare the frontend layout file being used in config.xml.

// app/code/local/Homesliders/Override/etc/config.xml

<?xml version="1.0"?>
<config>
    <frontend>
        <layout>
            <updates>
                <homesliders_override>
                    <file>homesliders_override.xml</file>
                </homesliders_override>
            </updates>
        </layout>
    </frontend>
</config>

And finally, the layout update moved correctly from local.xml to the override module’s layout file.

// app/design/frontend/base/default/layout/local.xml

<?xml version="1.0"?>
<layout>
    <catalog_category_view>
        <reference name="content">
            <block type="custom/homesliders" name="custom.homesliders.sliders" as="custom_sliders" template="homesliders/sliders.phtml" />
        </reference>
    </catalog_category_view>
</layout>

This way, if the homesliders module is ever disabled, the override will have to be disabled as well and therefore the block type errors will not be present in the logs.

Note: This article is based on Magento CE version 1.9.