I've created a custom module to display category thumbnail image.
I've read magento dev doc
It is displayed in my category section using category_form.xml
<?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="content">
<field name="thumbnail">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">string</item>
<item name="source" xsi:type="string">category</item>
<item name="label" xsi:type="string" translate="true">Thumbnail Image</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="formElement" xsi:type="string">fileUploader</item>
<item name="elementTmpl" xsi:type="string">ui/form/element/uploader/uploader</item>
<item name="previewTmpl" xsi:type="string">Magento_Catalog/image-preview</item>
<item name="required" xsi:type="boolean">false</item>
<item name="sortOrder" xsi:type="number">30</item>
<item name="uploaderConfig" xsi:type="array">
<item name="url" xsi:type="url" path="categorylist/category_thumbnailimage/upload"/>
</item>
</item>
</argument>
</field>
</fieldset>
</form>
when i upload image i showing following error
1 exception(s):
Exception #0 (BadMethodCallException): Missing required argument $baseTmpPath of Magento\Catalog\Model\ImageUploader.
Exception #0 (BadMethodCallException): Missing required argument $baseTmpPath of Magento\Catalog\Model\ImageUploader.
my controller file is
<?php
namespace Company\Categorylist\Controller\Adminhtml\Category\Thumbnailimage;
use Magento\Framework\Controller\ResultFactory;
/**
* Class Upload
*/
class Upload extends \Magento\Backend\App\Action
{
protected $baseTmpPath;
/**
* Image uploader
*
* @var \Magento\Catalog\Model\ImageUploader
*/
protected $imageUploader;
/**
* Upload constructor.
*
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Catalog\Model\ImageUploader $imageUploader
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Catalog\Model\ImageUploader $imageUploader
) {
$this->imageUploader = $imageUploader;
parent::__construct($context);
}
public function execute() {
try {
$result = $this->imageUploader->saveFileToTmpDir('thumbnail');
$result['cookie'] = [
'name' => $this->_getSession()->getName(),
'value' => $this->_getSession()->getSessionId(),
'lifetime' => $this->_getSession()->getCookieLifetime(),
'path' => $this->_getSession()->getCookiePath(),
'domain' => $this->_getSession()->getCookieDomain(),
];
} catch (\Exception $e) {
$result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
}
return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
}
}
Best Answer
you need to add this in the
di.xml
of your module.this is needed because the dependency of your controller called
imageUploader
is an instance of\Magento\Catalog\Model\ImageUploader
. This is just a general class for upload that does not have the base path and base tmp path set. So it does not know where to upload the files.But in the
etc/di.xml
file of the catalog module you have this virtual type defined:This means that
Magento\Catalog\CategoryImageUpload
class does not actually exist, but when this is used you will actually use an instance of the class you are expectingMagento\Catalog\Model\ImageUploader
but it has the required member vars (basePath
,baseTmpPath
andallowedExtensions
) already set.