3

I am creating a backend magento 2 module and when I specify in the layout file a custom block, it will show a blank page with this being added to the system.log

[2016-10-19 07:35:35] main.INFO: Cache file with merged layout: LAYOUT_adminhtml_STORE1_32fdcd7fcff058e6f791ea5b6050bd6b5 and handles default, widget_selectwidget_index: Please correct the XML data and try again. [] [] [2016-10-19 07:35:35] main.INFO: Cache file with merged layout: LAYOUT_adminhtml_STORE1_3a2e4822a98337283e39f7b60acf85ec9 and handles empty: Please correct the XML data and try again. [] [] [2016-10-19 07:35:35] main.CRITICAL: Broken reference: the 'logo' element cannot be added as child to 'header', because the latter doesn't exist [] [] [2016-10-19 07:35:35] main.CRITICAL: Broken reference: the 'global.search' element cannot be added as child to 'header', because the latter doesn't exist [] [] [2016-10-19 07:35:35] main.CRITICAL: Broken reference: the 'user' element cannot be added as child to 'header', because the latter doesn't exist [] []

.... etc etc etc ....

[2016-10-19 07:35:35] main.CRITICAL: Broken reference: the 'header.inner.right' tries to reorder itself towards 'header.inner.left', but their parents are different: 'header' and '' respectively. [] [] [2016-10-19 07:35:35] main.CRITICAL: Broken reference: the 'global.search' tries to reorder itself towards 'notification.messages', but their parents are different: 'header.inner.right' and '' respectively. [] [] [2016-10-19 07:35:35] main.INFO: Cache file with merged layout: LAYOUT_adminhtml_STORE1_36f1b068ec7ccf4878f9284dd1137afd1 and handles catalog_product_prices: Please correct the XML data and try again. [] []

If I specify my block class as class="Magento\Backend\Block\Template" then it will show the page with the correct template which leads me to think that there is no problem with the template or the rest of the module.

Here is the block (app/code/Vendor/Widget/Block/Adminhtml/SelectWidgetBlock.php) :

namespace Vendor\Widget\Block\Adminhtml;
use Magento\Backend\Block\Template;

class SelectWidgetBlock extends Template
{
    public function __construct(Template\Context $context, array $data = []) 
    {
        parent::__construct($context, $data);
    }

    public function greet()
    {
        return 'Hello world';
    }
}

This is the layout file (app/code/Vendor/Widget/view/adminhtml/layout/widget_selectwidget_index.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" layout="empty">
<head>
    <title>
        Widget
    </title>
</head>
<body>
    <referenceContainer name="content">
        <block class="Vendor\Widget\Block\Adminhtml\SelectWidgetBlock" name="vendor_widget.select" template="Vendor_Widget::selectwidget.phtml"/>
    </referenceContainer>
</body>

Now the phtml (app/code/Vendor/Widget/view/adminhtml/templates/selectwidget.phtml) :

<p>Here is the phtml file</p>

<?php echo $this->greet() ?>

Here is the controller just in case (app/code/Vendor/Widget/Controller/Adminhtml/SelectWidget/Index.php) :

namespace Vendor\Widget\Controller\Adminhtml\SelectWidget;

class Index extends \Magento\Backend\App\Action
{
    protected $resultPageFactory;

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

    public function execute()
    {
        return  $resultPage = $this->resultPageFactory->create();
    }
}

If you have any idea, please !

2 Answers 2

1

I solved my problem by removing layout="empty" from my layout file.

0

your file name and class name and xml class not matched

file name app/code/Vendor/Widget/Block/Adminhtml/SelectWidgetBlock.php

you xml will be

<referenceContainer name="content">
        <block class="Vendor\Widget\Block\Adminhtml\SelectWidgetBlock" name="vendor_widget.select" template="Vendor_Widget::selectwidget.phtml"/>
    </referenceContainer>
1
  • Sorry I had to make few change to the code before I copy past it here and I miswrote the class path. So the result is still the same with the same output in log file and the blank page. But thanks for highlighting it, I have updated the code in the original post.
    – Francois
    Commented Oct 19, 2016 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.