0

I have a module that primarily works with the admin-side of Magento 2.

At the minute, I'm just trying to parse a CSV file and log the contents, here's a skimmed version of my Block class:

app/code/Vendor/Module/Block/Adminhtml/Import.php

<?php
    namespace Vendor\Module\Block\Adminhtml;

    class Import extends \Magento\Framework\View\Element\Template
    {    
        public function parseFile($file)
        {
            foreach (array_map('str_getcsv', file($file)) as $num => $row)
            {
                $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/bob.log');
                $logger = new \Zend\Log\Logger();
                $logger->addWriter($writer);
                $logger->info(print_r($row, 1). PHP_EOL);
            }
        }
    }

Which I attempt to call in my (skimmed) Controller class:

app/code/Vendor/Module/Controller/Adminhtml/Import/Submit.php

class Submit extends \Magento\Framework\App\Action\Action
{
    protected $_resultLayoutFactory;

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

    public function execute()
    {
        $files = (array) $this->getRequest()->getFiles();

        if (!empty($files)) {
            $importFile = $files['import_file'];

            if (!$importFile['error']) {
                $resultLayout = $this->_resultLayoutFactory->create();

                $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/another.log');
                $logger = new \Zend\Log\Logger();
                $logger->addWriter($writer);
                $logger->info($resultLayout->getLayout()->getBlock('Vendor\Module\Block\Adminhtml\Import'));

            }
        }

        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }
}

Going to another.log show's:

2020-02-28T14:18:16+00:00 INFO (6):

I've ran:

$ php bin/magento setup:upgrade
$ php bin/magento setup:di:compile
$ php bin/magento setup:static-content:deploy -f
$ php bin/magento cache:flush

But to no avail. I turned on the With Block Hints option in debugging and it matches, so I'm not quite sure why my Controller can't see it.

How do I use my Block class in my Controller class (adminhtml side)?

1
  • Hello you must use createBlock not getBlock and if you want show something in log add ->toHtml() Commented Feb 28, 2020 at 19:50

1 Answer 1

1

To Call block, PLease update your Submit.php file as below:

Replace this code:

$logger->info($resultLayout->getLayout()->getBlock('Vendor\Module\Block\Adminhtml\Import'));

With this:

$logger->info($resultLayout->getLayout()->create()->createBlock('Vendor\Module\Block\Adminhtml\Import'));

Hope this help!

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.