13

Is it possible to change a block type in layout XML?

I know how to change template using setTemplate method, but I am trying to change a block's type. Is this possible?

  • please explain in details what you have to change – Keyul Shah Jan 26 '14 at 16:58
7

Changing the block type means changing the block class, so no, but...

... you can just overwrite the block:

  1. Find a layout handle which is invoked later OR be sure, that your layout.xml is processed after the block is created, e.g. with <depends> in Company_Module.xml
  2. Just create a new block of other type with the same name

The problem is, that this overwrites the block and does not change the type. This means everything which is done on this block between the creation and your change is lost.

For example for changing the catalog.navigation:

<layout version="0.1.0">
    <catalog_category_default translate="label">
        <reference name="left">
            <block type="myModule/navigation" name="catalog.leftnav" after="currency" template="myModule/catalog/navigation/left.phtml"/>
        </reference>
    </catalog_category_default>
</layout>
  • 1
    You can also create a new block class that "inherits" the block class you are looking to replace. You will need to add an entry in the config.xml of your custom extension to swap the block class with yours. This would make sense if you plan to extend or modify the block's functionality. – Biagio Arobba Mar 2 '16 at 16:50
3

You can't change it in the layout-files afaik. You can, however, extend and override them in your config-files.

Example:

<config>
    <global>
        <blocks>
            <catalog>
                <rewrite><product_view>My_Custom_Block_Product_View</product_view></rewrite>
            </catalog>
        </blocks>
    </global>
</config>

Remember to extend the original block.

1

I was facing similar issue that I need to update a specific block for a specific layout.

What I end up doing and it seems to work is (in the layout file)

<controller_x_y>
    <reference name="block_name_reference">
        <block type="mymodule/customblock" name="block_name_reference" />
    </reference>
</controller_x_y>

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.