Magento 2 Adminhtml – Fix Custom Block Blank Page Issue

adminhtmlmagento2

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 !

Best Answer

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>
Related Topic