Add link in admin form - Magento 2
-
08-10-2020 - |
Domanda
I am creating a module for check distance from store to various area. For this, I need to give sample csv file format to store managers so they can format their csv files and upload in admin-side.
I need to put that link besides file-browse button.
For that, I have created below code.
$fieldset->addField(
'csv_file',
'file',
[
'name' => 'csv_file',
'label' => __('Upload Csv File'),
'title' => __('Upload Csv File'),
'note' => '<br><span id="sample-file-span"
. '<a id="sample-file-link" href="#">'
. __('Download Sample File')
. '</a></span>',
]
);
By this, I can see the link.
My questions are,
Where I put my sample csv file in my module ?
What to write code in href attribute so store-manager can download it ?
Advanced Thanks !
Soluzione
You need to create custom type in form
$fieldset->addType('csvfile', '\Vendor\Module\Block\Adminhtml\Youblock\Renderer\Csvfile');
$fieldset->addField(
'file',
'csvfile',
[
'name' => 'csvfile',
'label' => __('csvfile'),
'title' => __('csvfile'),
]
);
app/code/Vendor/Module/Block/Adminhtml/Yourblock/Renderer/Csvfile.php
<?php
namespace Vendor\Module\Block\Adminhtml\Yourblock\Renderer;
use Magento\Framework\DataObject;
class Csvfile extends \Magento\Framework\Data\Form\Element\AbstractElement
{
protected $_assetRepo;
public function __construct(
\Magento\Framework\View\Asset\Repository $assetRepo
) {
$this->_assetRepo = $assetRepo;
}
public function getElementHtml()
{
$csvFile = $this->_assetRepo->getUrl('Vendor_Module::csv/yourfile.csv');
$csvLink = "<a href=".$csvFile." target='_blank'>Download Sample File</a>";
return $csvLink;
}
}
Copy yourfile.csv at
app/code/Vendor/Module/view/adminhtml/web/csv/yourfile.csv
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange