Skip to content

Magento 2.4 : Admin Custom page block not loading

I have created a custom backend page and need to show some custom content within the page. For that I have created

1. Controller/Adminhtml/Sync/Index.php
Here in this, getBlock() $block = $resultPage->getLayout()->getBlock('odoo.sync'); returns a false value and, output rendered with neither default backend layout nor mentioned custom block. A plain page without backend layout is loaded.

<?php
namespace IwdatStockManager235ControllerAdminhtmlSync;

use MagentoCatalogModelResourceModelProductCollectionFactory;
use PsrLogLoggerInterface;
use MagentoFrameworkAppConfigScopeConfigInterface;
use MagentoInventorySalesAdminUiModelGetSalableQuantityDataBySku; 
use MagentoCatalogModelProduct;
use MagentoCatalogInventoryApiStockRegistryInterface;
use MagentoCatalogModelProductRepository;
use MagentoInventoryApiApiDataSourceItemInterfaceFactory;        
use MagentoInventoryApiApiSourceItemsSaveInterface;
use MagentoFrameworkControllerResultFactory;
use MagentoFrameworkIndexerIndexerInterfaceFactory;
use MagentoFrameworkIndexerIndexerRegistry;
use MagentoFrameworkAppActionHttpGetActionInterface;

class Index extends MagentoBackendAppAction implements HttpGetActionInterface
{

    protected $resultPageFactory = false;  
    protected $indexerInterfaceFactory;
    protected $indexerRegistry; 

    public function __construct(
        MagentoBackendAppActionContext $context,
        MagentoFrameworkViewResultPageFactory $resultPageFactory,        
        LoggerInterface $logger,            
        CollectionFactory $productCollectionFactory,
        ScopeConfigInterface $scopeConfig,
        Product $product,    
        GetSalableQuantityDataBySku $getSalableQuantityDataBySku,
        StockRegistryInterface $StockRegistryInterface,
        ProductRepository $productRepository,
        SourceItemInterfaceFactory $sourceItemFactory,
        SourceItemsSaveInterface $sourceItemsSaveInterface,
        MagentoFrameworkHTTPClientCurl $curl, 
        IndexerInterfaceFactory $indexerInterfaceFactory,
        IndexerRegistry $indexerRegistry
    ) {
            parent::__construct($context);
            $this->resultPageFactory = $resultPageFactory;
            .
            .
            .
    }

    public function getProductCollection()
    {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        //$collection->setPageSize(10); // fetching only 10 products

        return $collection;
    }

    public function execute()
    {
        
        $resultPage = $this->resultPageFactory->create(ResultFactory::TYPE_PAGE);
        $resultPage->getConfig()->getTitle()->prepend('Odoo Sync');
        $block = $resultPage->getLayout()->getBlock('odoo.sync');

        .
        .
        .

        return $resultPage;
    }

}

2. view/adminhtml/layout/odoo_sync.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="MagentoBackendBlockTemplate" name="odoo.sync" template="Iwdat_StockManager235::odoo_sync.phtml"/>
        </referenceContainer>
    </body>
</page>

3. view/adminhtml/templates/odoo_sync.phpml

<h2>Template Here </h2>

Can someone please guide me on what changes I should do get the block loaded in to custom backend page.

Thanks and Best Regards

Indunil