Magento 2 – Fix Custom Module Layout Not Working

magento2module

I added custom link in header tab example chat, if i click this link move one custom form, but, its only show only 1column layout, form not showing. what I miss this code.

This is my layout File:

<?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>
    <referenceContainer name="content">
    <block class="ChennaiBox\Chat\Block\ChatBlock" template="ChennaiBox_Chat::ChatForm.phtml"/>
    </referenceContainer>

  </body>
  </page>

This is My Block:

 <?php

  namespace ChennaiBox\Chat\Block;
  class ChatBlock extends \Magento\Framework\View\Element\Template  
   {


   }

This is Template File:

 <?php?>
<h1>Chat</h1>

This is my default.xml layout:

  <?xml version="1.0"?>

  <page layout="3columns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="default_head_blocks"/>
     <body>
      <move element="Chat_link" destination="header.links"/>
        <referenceContainer name="header.panel">
        <block class="Magento\Framework\View\Element\Html\Link\Current" name="Chat_link">
            <arguments>
                <argument name="label" xsi:type="string" translate="true">Chat</argument>
                <argument name="path" xsi:type="string">chennaibox/livechat/livechat</argument>
            </arguments>
           </block> 
 </referenceContainer>
 </body>
 </page>

This is my controller:

 <?php

 namespace ChennaiBox\Chat\Controller\LiveChat;
  class LiveChat extends \Magento\Framework\App\Action\Action
   {

 protected $resultPageFactory;

 public function __construct(\Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $page)
    {
      parent::__construct($context);
      $this->resultPageFactory = $page;       
    }
public function execute()
{
    $resultPage = $this->resultPageFactory->create();
    return $resultPage;

   //echo "This is my test";
}

}

I need to print "Chat" word in layout, but its only show 1 column layout what i miss. suggest me how to do this…

Best Answer

You can just 3columns in your layout file instead of 1column.

Try below code,

<?xml version="1.0"?>

  <page layout="3columns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="default_head_blocks"/>
     <body>
        <referenceContainer name="header.panel">
        <block class="ChennaiBox\Chat\Block\ChatBlock" name="Chat_link">
            <arguments>
                <argument name="label" xsi:type="string" translate="true">Chat</argument>
                <argument name="path" xsi:type="string">chennaibox/livechat/livechat</argument>
            </arguments>
           </block> 

      <move element="Chat_link" destination="header.links"/>
 </referenceContainer>
 </body>
 </page>

inside your block,

ChatBlock.php file,

namespace ChennaiBox\Chat\Block;

/**
 * Class Link
 *
 * @SuppressWarnings(PHPMD.DepthOfInheritance)
 */
class ChatBlock extends \Magento\Framework\View\Element\Html\Link
{
    protected $_template = 'ChennaiBox_Chat::ChatForm.phtml';    



    public function getLabel()
    {        
        return __('Chat');
    }
}