0

Hi i have custom import pincode option in my module but i want to include a download sample file just below the upload field but am not sure how to achieve this enter image description here

Importpincode

<?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
{
    /**
     * Prepare form
     *
     * @return $this
     */
    protected function _prepareForm()

    {


        $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'
            ]
        );
        $fieldset->addField(
            'file',
            'file',
            [
                'name'  => 'file',
                'label' => __('Upload File'),
                'title' => __('Upload File'),
                'required' => true,
            ]
        );
        $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;
    }
}

Form.php

<?php

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

class Form extends \Magento\Backend\Block\Widget\Form\Generic
{
    /**
     * Prepare form
     *
     * @return $this
     */
    protected function _prepareForm()
    {
        /** @var \Magento\Framework\Data\Form $form */
        $form = $this->_formFactory->create(
            [
                'data' => [
                    'id' => 'edit_form',
                    'action' => $this->getData('action'),
                    'method' => 'post',
                    'enctype' => 'multipart/form-data'
                ]
            ]
        );
        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }
}

any help on this will be appreciated

4
  • Please avoid to used objectManger .
    – Msquare
    Commented Feb 15, 2020 at 11:36
  • this is the demand
    – Pramod
    Commented Feb 15, 2020 at 11:37
  • can you explain based on this code
    – Pramod
    Commented Feb 15, 2020 at 11:37
  • i update your file please check it
    – Msquare
    Commented Feb 15, 2020 at 11:45

1 Answer 1

0
<?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;
        }
    }

put your sample file into your module give path (Filename must be same)

app/code/VendoreName/ModuleName/view/adminhtml/web/img

Here this line

$path = $this->_assetRepo->getUrl("VendoreName_ModuleName::img/your_sample_file_name.csv")

This line generate an URL. The Sample file which are in Our VendoreName_ModuleName in above path. And when you click on this generated link this download your sample file which are place into your module.

When you change in __construct() add / remove class you need to run this command.

php bin/magento s:up

php bin/magento s:s:d -f 

php bin/magento c:f

I Hope This Helps You.

12
  • $path = $this->_assetRepo->getUrl("VendoreName_ModuleName::img/your_sample_file_name.csv");
    – Pramod
    Commented Feb 15, 2020 at 11:20
  • could you please explain this line
    – Pramod
    Commented Feb 15, 2020 at 11:21
  • Update answer for more explaining about sample file url. Let me inform above solution is works for you ?
    – Msquare
    Commented Feb 15, 2020 at 11:29
  • wait i will update my question
    – Pramod
    Commented Feb 15, 2020 at 11:33
  • Let it check answer is updated base on your updated Question and also inform me it's works for you ?
    – Msquare
    Commented Feb 15, 2020 at 11:53

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.