Magento – How to extend layout in Custom module

layoutmagento-2.0

I have extended module-catalog block in Test_Catalog (Test is my vendor name and Catalog is module name, in which I am extending block of module-catalog). I want to add small table element on product view page for which i have created catalog_product_view.xml (location : root\app\code\Test\Catalog\view\frontend\layout\catalog_product_view.xml) But still its not displaying any content from test.phtml file . I dont want to change entire look of product view ,I just want to add small table element. Below are some things I have tried in catalog_page_view.xml:

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
      <block class="Test\Catalog\Block\Rewrite\Product\View" name="product.seller.list" as="product_seller_list" template="test.phtml" />
    </body>
</page>

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Test\Catalog\Block\Rewrite\Product\View" name="product.seller.list" template="test.phtml" />
        </referenceContainer>
    </body>
</page>

system.log

[2016-04-18 15:03:34] main.CRITICAL: Invalid template file: 'test.phtml' in module: 'Magento_Catalog' block's name: 'content_schedule_block5' [] []

Best Answer

Here is a simple module that displays static static html content on the product page. You can try it. It 100% works on my local Magento 2 test environment.

Base folder: app\code

Directory tree:

└── Test
    └── Catalog
        ├── Block
        │   └── Rewrite
        │       └── Product
        │           └── View.php
        ├── composer.json
        ├── etc
        │   └── module.xml
        ├── registration.php
        └── view
            └── frontend
                ├── layout
                │   └── catalog_product_view.xml
                └── templates
                    └── test.phtml

File: Test/Catalog/composer.json

{
    "name": "test/catalog",
    "description": "Test module for Magento 2",
    "type": "magento2-module",
    "version": "1.0.0",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "require": {
        "php": "~5.5.0|~5.6.0|~7.0.0",
        "magento/module-catalog": "~100.0"
    },
    "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "Test\\Catalog\\": ""
        }
    }
}

File: Test/Catalog/registration.php

<?php

use \Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Test_Catalog', __DIR__);

File: Test/Catalog/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Test_Catalog" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

File: Test/Catalog/view/frontend/layout/catalog_product_view.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Test\Catalog\Block\Rewrite\Product\View" name="product.seller.list" template="test.phtml" />
        </referenceContainer>
    </body>
</page>

File: Test/Catalog/Block/Rewrite/Product/View.php

<?php

namespace Test\Catalog\Block\Rewrite\Product;

class View extends \Magento\Catalog\Block\Product\View
{

}

File: Test/Catalog/view/frontend/templates/test.phtml

!!!!some static content!!!!
Related Topic