Magento layout will replace block with another block

I want to replace a block in the standard catalog.xml with one custom type block.

<block type="catalog/product_view_attributes" name="product.attributes" as="additional" template="catalog/product/view/attributes.phtml">
    <action method="addToParentGroup"><group>detailed_info</group></action>
    <action method="setTitle" translate="value"><value>Additional Information</value></action>
</block>

      

I want to replace the above standard with my own block type below , but using the same "name".

<block type="attributesasgroup/groupview" name="product.attributes" as="additional" template="webguys/attributesasgroup/groupview.phtml">
    <action method="addToParentGroup"><group>detailed_info</group></action>
    <action method="setTitle" translate="value"><value>Additional Information</value></action>
</block>

      

Can anyone advise me on the best way to do this? I don't know what XML actions and methods to use. Do I need to delete the previous block? unsetChild? IF I add a block with the same "name" and the same parent, does it overwrite the existing one with the same name?

+3


source to share


2 answers


I will address every doubt you have here.

1. Can I replace the default block with my custom block as shown above?

Of course you can. You can use your own block instead of the default block with the above code

2. What is the best way to do this?

I prefer to use local.xml

for this purpose. local.xml

is a special layout file that will always be processed by Magento. i e, the layout updates specified in this file will be reviewed and applied finally after all other layout update files are saved. Thus, it is an easy way to change any layout changes. So include this file in your theme layout directory with the specified layout changes.

File: app/design/frontend/<package>/<theme>/layout/local.xml



<catalog_product_view>

    <remove name="product.attributes" />

    <reference name="content">
        <block type="attributesasgroup/groupview" name="product.attributes" as="additional" template="webguys/attributesasgroup/groupview.phtml">
            <action method="addToParentGroup"><group>detailed_info</group></action>
            <action method="setTitle" translate="value"><value>Additional Information</value></action>
        </block>
    </reference>

</catalog_product_view>

      

The layout that ours maintains local.xml

is so simple. It first removes the default block from the layout structure. To use this, we used remove

node. Then we added our own block to the content piece.

It is important here that your block is defined server side. If it is not defined, Magento will not know which block you are linking to, so magneto will not display content inside your block. So make sure it is defined in the backend. In the above case, in order to properly process your block, Magento needs Namespace_Modulename_Block_Groupview

to define in app/code/local/Namespace/Modulename/Block/Groupview.php

(where Namespace_Modulename

stands for your custom module name)

3. IF I add a block with the same "name" and the same parent, does it overwrite the existing one with the same name?

You can of course provide a default block name. In purple, each block must have a unique name . To obey this rule, you must first remove the default block. This will allow us to use this block name in our own block. Otherwise Magento will behave badly.

Note. Two actions apply to your blocks. They addToParentGroup

and setTitle

. Set your block to a group first detailed_info

and second to set a title for your block.

+2


source


Do you mean this?

<reference name="product.attributes">

  <action method="setTemplate">

     <template>webguys/attributesasgroup/groupview.phtml</template>

   </action>

     <action method="addToParentGroup"><group>detailed_info</group></action>
    <action method="setTitle" translate="value"><value>Additional Information</value>

</reference>

      



we just replace a new template for the block instance, keeping all the same values as the attributes of that name

, as

and the other

Source : http://www.magentocommerce.com/design_guide/articles/intro-to-layouts#head-anatomy-of-a-layout

+1


source







All Articles