What I would like to do is add a field to an admin page where a file can be downloaded and uploaded. A client would like to upload a csv file that is used by a module developt by an agency that was developing the site before us.
What I have is that I have been able to add a page to the backend, a download and an upload field.
<!-- app/code/Vendor/ModuleName/etc/adminhtml/system.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="modulename" sortOrder="500" showInDefault="1" showInStore="1" showInWebsite="1">
<tab>catalog</tab>
<label>Module Name</label>
<group id="general" translate="label" showInDefault="1" showInWebsite="1" showInStore="1">
<label>General</label>
<field id="download" translate="label" type="VendorModuleNameBlockAdminhtmlFormFieldDownload" sortOrder="5" showInDefault="1">
<label>Download</label>
</field>
<field id="upload_plates_file" translate="label" type="VendorModuleNameBlockAdminhtmlFormFieldUpload" sortOrder="6" showInDefault="1">
<label>Upload</label>
</field>
</group>
</section>
</system>
</config>
And the two files; Upload.php
and Download.php
.
// app/code/Vendor/ModuleName/Block/Adminhtml/Form/Field/Upload.php
class Upload extends MagentoFrameworkDataFormElementAbstractElement
{
[...__construtor()...]
protected function _construct(): void
{
parent::_construct();
$this->setType('file');
}
public function getElementHtml(): string
{
return parent::getElementHtml();
}
}
// app/code/Vendor/ModuleName/Block/Adminhtml/Form/Field/Download.php
class Download extends MagentoFrameworkDataFormElementAbstractElement
{
[...__constructor()...]
public function getElementHtml(): string
{
$buttonBlock = $this->getForm()->getParent()->getLayout()->createBlock(Button::class);
$params = ['website' => $buttonBlock->getRequest()->getParam('website')];
$url = $this->backendUrl->getUrl("*/*/exportPlatesCsv", $params);
$data = [
'label' => __('Export CSV'),
'onclick' => "setLocation('" .
$url .
"somewhere/somefile.csv' )",
'class' => '',
];
return $buttonBlock->setData($data)->toHtml();
}
}
Now I understand that I need controllers for the actual upload and download action, and I have a app/code/Vendor/ModuleName/Controller/Adminhtml/System/Config/Download.php
and a app/code/Elgentos/DownUploadPlatesCSV/Model/Config/Backend/Upload.php
. And I have some scafolding inside there, but it does nothing and I wouldn’t know where to begin to fix it.
I know this perhaps isn’t a lot to go by and debugging something on a system you don’t have access to is next to impossible but a nudge in the right direction or an article that covers this use case, which I have not been able to find, would be wonderful! (BTW using magento 2.4.5-p1)