Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update Block Type in layout XML updates

Tags:

magento

I am trying to update the "type" parameter of an existing Block in my theme layout XMLs. In example, I would like to use the Block "catalog/rewrite_navigation" instead of "catalog/navigation" for the reference name "catalog.topnav".

I've tested several ways by reading thru core PHP files for layout, blocks, updates and so on but didn't succeed. I just would love to avoid using unsetChild and then recreating a block.

The "regular" way would be :

    <reference name="top.menu">
        <action method="unsetChild"><name>catalog.topnav</name></action>
        <block type="catalog/rewrite_navigation" name="catalog.topnav" template="catalog/navigation/top.phtml"/>
    </reference>

But do you guys have a clue to do something like this instead :

    <reference name="catalog.topnav">
        <action method="setType"><name>catalog/rewrite_navigation</name></action>
    </reference>

With such an update the block type of the catalog.topnav would be updated from "catalog/navigation" to "catalog/rewrite_navigation".

Thanks a lot for your ideas !

like image 590
Hervé Guétin Avatar asked Apr 28 '11 21:04

Hervé Guétin


People also ask

What is the correct way to inject CMS block in a layout?

Reference a CMS block A CMS block is injected into the layout by using the Magento/Cms/Block/Block class with the block_id argument. Any block or container can be used as a reference. As a result, the CMS block added to the bottom of the page.

What is reference block in Magento 2?

ReferenceBlock: ReferenceBlock is used when we want to use already existing block and want to put our block in that existing block. We use this if we want to add changes to phtml file of that block.


1 Answers

I have never seen a syntax like this, and I do know that Magento creates the object before running actions on it (because it needs to have an instance before it can execute methods on that instance). That leads me to believe that there is no way to do this using actions.

Also beware of trying to do it by unsetting the block and adding it again. By the time your layout executes, other blocks may have already added children to that block, and they will simply fall off when you remove the block.

The canonical way to do this is simply to override the actual catalog/navigation block, so that it returns the class that you intend it to. If, for some reason, this doesn't work for you (e.g. someone else already overrode the block), you may need to modify the XML file to reflect your new class handle.

Hope that helps!

like image 107
Joe Mastey Avatar answered Oct 20 '22 08:10

Joe Mastey