0

Hi i am trying to add a custom import fucntionality for my module but I got this error:-

Fatal error: Uncaught Error: Call to a member function getUrl() on null in E:\xampp\htdocs\m2\vendor\magento\framework\View\Element\AbstractBlock.php:775 Stack trace: #0 E:\xampp\htdocs\m2\vendor\magento\module-backend\Block\Widget\Tabs.php(141): Magento\Framework\View\Element\AbstractBlock->getUrl() #1 [internal function]: Magento\Backend\Block\Widget\Tabs->addTab('importpincode', 'netsmartz_pinco...') #2 E:\xampp\htdocs\m2\vendor\magento\framework\View\Layout\Generator\Block.php(298): call_user_func_array(Array, Array) #3 E:\xampp\htdocs\m2\vendor\magento\framework\View\Layout\Generator\Block.php(169): Magento\Framework\View\Layout\Generator\Block->generateAction(Object(Netsmartz\PincodeChecker\Block\Adminhtml\Importpincode\Edit\Tabs), 'addTab', Array) #4 E:\xampp\htdocs\m2\vendor\magento\framework\View\Layout\GeneratorPool.php(81): Magento\Framework\View\Layout\Generator\Block->process(Object(Magento\Framework\View\Layout\Reader\Context), Object(Magento\Framework\View\Layout\Generator\Context)) #5 E:\xampp\htdocs\m2\ve in E:\xampp\htdocs\m2\vendor\magento\framework\View\Element\AbstractBlock.php on line 775

here is my code:-

<?php

namespace vendor\module\Block\Adminhtml\Importpincode\Edit\Tab;

class Importpincode extends \Magento\Backend\Block\Widget\Form\Generic implements \Magento\Backend\Block\Widget\Tab\TabInterface
{   

    protected $_assetRepo;

    public function __construct(
       \Magento\Framework\View\Asset\Repository $assetRepo
    ) {
        $this->_assetRepo = $assetRepo;
    }
    /**
     * Prepare form
     *
     * @return $this
     */
    protected function _prepareForm()

    {

        $path = $this->_assetRepo->getUrl("vendor_module::importsample/sample.csv");

        $form = $this->_formFactory->create();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $storemanager = $objectManager->create('Magento\Store\Model\StoreManagerInterface');

        $fieldset = $form->addFieldset(
            'base_fieldset',
            [
                'legend' => __('Import Pincodes'),
                'class'  => 'fieldset-wide'
            ]
        );

        $importdata_script  = $fieldset->addField(
            'file',
            'file',
            [
                'name'  => 'file',
                'label' => __('Upload File'),
                'title' => __('Upload File'),
                'required' => true,
            ]
        );

        $importdata_script->setAfterElementHtml("
        <span id='sample-file-span' ><a id='sample-file-link' href='".$path."'  >Download Sample File</a></span>
        ");

        $this->setForm($form);
        return parent::_prepareForm();
    }

    /**
     * Prepare label for tab
     *
     * @return string
     */
    public function getTabLabel()
    {
        return __('Import Pincodes');
    }

    /**
     * Prepare title for tab
     *
     * @return string
     */
    public function getTabTitle()
    {
        return $this->getTabLabel();
    }

    /**
     * Can show tab in tabs
     *
     * @return boolean
     */
    public function canShowTab()
    {
        return true;
    }

    /**
     * Tab is hidden
     *
     * @return boolean
     */
    public function isHidden()
    {
        return false;
    }
}

any help on this will be great

update:- enter image description here

2
  • have you tried by calling parent constructor as @Msquare answer? – Ashish Ranade Feb 17 '20 at 9:28
  • yeah check the updated question – Pramod Feb 17 '20 at 9:45
6

try this

<?php

namespace vendor\module\Block\Adminhtml\Importpincode\Edit\Tab;

class Importpincode extends \Magento\Backend\Block\Widget\Form\Generic implements \Magento\Backend\Block\Widget\Tab\TabInterface
{
    protected $_assetRepo;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        \Magento\Framework\View\Asset\Repository $assetRepo,
        array $data = []
    ) {
        $this->_assetRepo = $assetRepo;
        parent::__construct($context, $registry, $formFactory, $data);
    }

    /**
     * Prepare form
     *
     * @return $this
     */
    protected function _prepareForm()

    {

        $path = $this->_assetRepo->getUrl("vendor_module::importsample/sample.csv");

        $form = $this->_formFactory->create();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $storemanager = $objectManager->create('Magento\Store\Model\StoreManagerInterface');

        $fieldset = $form->addFieldset(
            'base_fieldset',
            [
                'legend' => __('Import Pincodes'),
                'class'  => 'fieldset-wide'
            ]
        );

        $importdata_script  = $fieldset->addField(
            'file',
            'file',
            [
                'name'  => 'file',
                'label' => __('Upload File'),
                'title' => __('Upload File'),
                'required' => true,
            ]
        );

        $importdata_script->setAfterElementHtml("
        <span id='sample-file-span' ><a id='sample-file-link' href='".$path."'  >Download Sample File</a></span>
        ");

        $this->setForm($form);
        return parent::_prepareForm();
    }

    /**
     * Prepare label for tab
     *
     * @return string
     */
    public function getTabLabel()
    {
        return __('Import Pincodes');
    }

    /**
     * Prepare title for tab
     *
     * @return string
     */
    public function getTabTitle()
    {
        return $this->getTabLabel();
    }

    /**
     * Can show tab in tabs
     *
     * @return boolean
     */
    public function canShowTab()
    {
        return true;
    }

    /**
     * Tab is hidden
     *
     * @return boolean
     */
    public function isHidden()
    {
        return false;
    }
}

above code running good

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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