I am working on a module to allow a price matrix to be assigned to a product.
This required a fileuploader field to be added to the product edit form.
I have managed to add the field to the product edit form, created a controller which handles the upload and returns some json to the product edit form, I can see the preview template appear with the correct data in, but when I then try and save the form, this data is not passed within the save request.
UI Component
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="pricematrix">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Price Matrix</item>
<item name="collapsible" xsi:type="boolean">true</item>
<item name="sortOrder" xsi:type="number">100</item>
<item name="dataScope" xsi:type="string">data.product</item>
<item name="provider" xsi:type="string">product</item>
<item name="ns" xsi:type="string">product_form</item>
</item>
</argument>
<field name="price_matrix">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">string</item>
<item name="label" xsi:type="string" translate="true">Upload Price Matrix</item>
<item name="formElement" xsi:type="string">fileUploader</item>
<item name="elementTmpl" xsi:type="string">ui/form/element/uploader/uploader</item>
<item name="sortOrder" xsi:type="number">1</item>
<item name="scopeLabel" xsi:type="string">[STORE]</item>
<item name="source" xsi:type="string">product</item>
<item name="required" xsi:type="boolean">false</item>
<item name="placeholderType" xsi:type="string">document</item>
<item name="uploaderConfig" xsi:type="array">
<item name="url" xsi:type="url" path="hub_pricematrix/upload/pricematrix"/>
</item>
</item>
</argument>
</field>
</fieldset>
</form>
AJAX Handler
<?php
namespace Hub\Pricematrix\Controller\Adminhtml\Upload;
use Magento\Backend\App\Action;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\MediaStorage\Model\File\UploaderFactory;
class Pricematrix extends Action
{
protected $fileSystem;
protected $uploaderFactory;
protected $resultRawFactory;
protected $allowedExtensions = ['csv'];
protected $fileId = 'price_matrix';
public function __construct(
Action\Context $context,
Filesystem $fileSystem,
UploaderFactory $uploaderFactory,
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory
) {
$this->fileSystem = $fileSystem;
$this->uploaderFactory = $uploaderFactory;
$this->resultRawFactory = $resultRawFactory;
parent::__construct($context);
}
public function execute()
{
$destinationPath = $this->getDestinationPath();
try {
$uploader = $this->uploaderFactory->create(['fileId' => $this->fileId])
->setAllowCreateFolders(true)
->setAllowedExtensions($this->allowedExtensions)
->addValidateCallback('validate', $this, 'validateFile');
$result = $uploader->save($destinationPath);
if (!$result) {
throw new LocalizedException(
__('File cannot be saved to path: $1', $destinationPath)
);
}
unset($result['tmp_name']);
unset($result['path']);
$result['url'] = "";
$response = $this->resultRawFactory->create();
$response->setHeader('Content-type', 'text/plain');
$response->setContents(json_encode($result));
return $response;
} catch (\Exception $e) {
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
}
}
public function validateFile($filePath)
{
// todo
}
public function getDestinationPath()
{
return $this->fileSystem
->getDirectoryWrite(DirectoryList::TMP)
->getAbsolutePath('/');
}
}
Backend showing an uploaded CSV