Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place my custom block inside another block in Magento using layout xml?

Tags:

magento

first of all I want to say that I have searched for this whole day on te internet and could not find what I wanted. I am a newbie here as well, so please forgive me if I broke any rule.

I am trying to develop a module which will add videos to product page along with images. I am stuck in this concept:

How do I insert my block into an existing base block ? For example, in the product page, there is a block product.info. Inside this block there is "Availability", "Price" etc. How do I insert my custom block just below "Availability" and above "Prices" using my module's layout xml and template.

So I am trying to achieve something like this using my module's layout file:

 <catalog_product_view translate="label">
        <reference name="content">
            <reference name="product.info">
                WRITE BLOCK HERE SO THAT MY BLOCK SHOWS BELOW AVAILABLITY
            </reference>
        </reference>
    </catalog_product_view>

Is this possible ? or Do I have to override the core class Mage_Catalog_Block_Product_View to do this ?

PS: Basically my aim is to list my videos next to images. Right now, I am able to list my videos from module, but images don't come in that case. I used

<block type="myblock/myblock" name="somename" as="media" template="abc.phtml"/>

So I want to append my block to the existing content.

like image 311
Hashid Hameed Avatar asked Oct 02 '22 12:10

Hashid Hameed


People also ask

How do I move a block in XML in Magento 2?

For moving a block to another destination, we need to use the <move> tag with a specific element name and destination place. <move> Tag will set the declared block or container element as a child of another element in the specified order.

How do you call CMS block in Magento 2 using layout XML?

Except for displaying CMS block on CMS Page or CMS Block, showing it on category page there is an option to call CMS block programmatically using XML file. You need to replace "my_cmsblock_identifier" with your CMS block Identifier or ID (we recommend to use Identifier).

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

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.

How do I override layout XML in Magento 2?

xml . To override page layout files, use the page_layout directory name instead of layout .


2 Answers

I solved it. I had to rewrite the Mage_Catalog_Block_Product_View_Media .

In my class I over-rid the function _toHtml function like this:

public function _toHtml()
{
    $html = parent::_toHtml();
    $html.=$this->getChildHtml('media_video');
    return $html;
}

where "media_video" is my block. My layout xml file:

<catalog_product_view translate="label">
    <reference name="content">
        <reference name="product.info">
            <reference name="product.info.media">
            <block type="myblock/myblock" name="somename" as="media_video" template="beta/abc.phtml"
                   before="-"/>
            </reference>
        </reference>
    </reference>
</catalog_product_view>
like image 158
Hashid Hameed Avatar answered Oct 12 '22 10:10

Hashid Hameed


You can add new block instead of over-right existing.

 <catalog_product_view translate="label">
    <reference name="content">
        <reference name="product.info">
    <block type="myblock/myblock" name="somename" as="media_new" template="abc.phtml"/>
        </reference>
    </reference>
</catalog_product_view>

Get New Block using following code in phtml file

<?php echo $this->getChildHtml('media_new') ?>

Thanks

like image 39
Jack G. Avatar answered Oct 12 '22 12:10

Jack G.