0

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 Iwdat\StockManager235\Controller\Adminhtml\Sync;

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku; 
use Magento\Catalog\Model\Product;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\Catalog\Model\ProductRepository;
use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory;        
use Magento\InventoryApi\Api\SourceItemsSaveInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Indexer\IndexerInterfaceFactory;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Framework\App\Action\HttpGetActionInterface;

class Index extends \Magento\Backend\App\Action implements HttpGetActionInterface
{

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

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,        
        LoggerInterface $logger,            
        CollectionFactory $productCollectionFactory,
        ScopeConfigInterface $scopeConfig,
        Product $product,    
        GetSalableQuantityDataBySku $getSalableQuantityDataBySku,
        StockRegistryInterface $StockRegistryInterface,
        ProductRepository $productRepository,
        SourceItemInterfaceFactory $sourceItemFactory,
        SourceItemsSaveInterface $sourceItemsSaveInterface,
        \Magento\Framework\HTTP\Client\Curl $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="Magento\Backend\Block\Template" 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

1 Answer 1

1

Everything seems correct in your code try adding cacheable="false" in your XML file if it is a cache issue.

<?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="Magento\Backend\Block\Template" name="odoo.sync" template="Iwdat_StockManager235::odoo_sync.phtml" cacheable="false" />
        </referenceContainer>
    </body>
</page>

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.