Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Block Injction via module.xml Files

Tags:

php

xml

magento

In a module I am writing I want to use using mymodule.xml to insert my own block after this block:

<block type="catalog/product_view_media" name="product.info.media" as="media" template="catalog/product/view/media.phtml"/>

which is nested within

<reference name="content">
    <block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">

as can be seen in app/design/frontend/base/default/layout/catalog.xml

I have tried many variants such as:

<reference name="content">
   <reference name="product.info">
      <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class"></block>
   </reference>
</reference>

and just

<reference name="product.info">
   <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class"></block>
</reference>

and

<reference name="product.info">
   <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class"></block>
</reference>

but I cant seem to find the correct combination. the only one that renders my block is:

<reference name="content">
   <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class"></block>
</reference>

but obviously it is not placed where I wanted it to be placed.

like image 760
epeleg Avatar asked Jan 10 '11 21:01

epeleg


1 Answers

If you look at the declaration of the content block up in page.xml, you'll see the following.

<block type="core/text_list" name="content" as="content" translate="label">

By default, the block named content is a core/text_list, which translates to a Mage_Core_Block_Text_List.

The purpose of a core/text_list blocks is simple. They automatically render any blocks inserted into them. That's why you can insert a block into content successfully.

The block you want to insert into

<block type="catalog/product_view" name="product.info" template="mymodule/folder/class.phtml" ...

is a catalog/product_view, which translates to a Mage_Catalog_Block_Product_View, which ultimately inherits from Mage_Core_Block_Template. That makes it a Template block. Template blocks do not automatically render their children. Template blocks will render a phtml file. If that phtml contains a call to

$this->getChildHtml('block_name');

then the block with that specific name will be rendered. If the phtml file contains a call to

$this->getChildHtml(); //no arguments

then all the child blocks will be rendered.

So, when you say

<reference name="product.info">
   <block type="mymodule/folder_class" after="media" name="mymodule.folder.class" template="mymodule/folder/class.phtml" as="mymodule_folder_class"></block>
</reference>

you're inserting a block of type mymodule/folder_class into the product.info block. But, because product.info is a template block and not a text list block, it doesn't render the block you inserted. You'll need to add a custom catalog/product/view.phtml template to your theme (by copying the base template), and then at the bottom of view.phtml, add

<?php echo $this->getChildHtml('mymodule.folder.class');?>
like image 86
Alan Storm Avatar answered Oct 26 '22 03:10

Alan Storm