I have a custom ImageUploader
Issue : When I try to create a new Object, the image field appears and works fine. Image send to my media dir and the name saved in database.
But, when I try to edit the previously created object, my objet has all his fields registered; but the image field is missing
Here comes the field in ui component
<field name="image" sortOrder="40" formElement="imageUploader">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">miseenavant</item>
</item>
</argument>
<settings>
<elementTmpl>ui/form/element/uploader/image</elementTmpl>
<item name="previewTmpl" xsi:type="string">Cpy_MiseEnAvant/image-preview</item>
<dataType>string</dataType>
<label translate="true">Mise En Avant Image</label>
<visible>true</visible>
<required>false</required>
</settings>
<formElements>
<imageUploader>
<settings>
<required>false</required>
<uploaderConfig>
<param xsi:type="url" name="url" path="mev/image/upload"/>
</uploaderConfig>
<openDialogTitle>Media Gallery</openDialogTitle>
<initialMediaGalleryOpenSubpath>mev_image/upload</initialMediaGalleryOpenSubpath>
<allowedExtensions>jpg jpeg gif png</allowedExtensions>
<maxFileSize>4194304</maxFileSize>
</settings>
</imageUploader>
</formElements>
</field>
It seems to be related to my dataprovider
public function getData()
{
$data = [];
if($id = $this->request->getParam('id')) {
$mev = $this->mevFactory->create()->load($id);
$data[$id] = $mev->getData();
}
try {
if(isset($data[$id])){
$currentStore = $this->storeManager->getStore();
$mediaUrl = $currentStore->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA);
$data[$id]['image'] = array($mediaUrl.$this->mevHelper::MEV_MEDIA_DIRECTORY.$data[$id]['image']);
}
} catch (NoSuchEntityException $e) {
var_dump($e->getMessage());
}
return $data;
}
Looks like i’m sending an array as image when it should be an object probably, cause I see this error in console :
file-uploader.js:171 Uncaught TypeError: Cannot create property
‘previewType’ on string
‘http://mywebsite/media/mev_image/upload/60d2f7eb55bd8.png
Edit : There is is the best I can do digging :/
$this->image->prepareDataSource(
[
'initialMediaGalleryOpenSubpath' => 'mev_image/upload',
'name' => $data[$id]['image'],
'file' => $data[$id]['image'],
'previewType' => 'image',
'type' => 'image/png',
'path' => $mediaUrl.$this->mevHelper::MEV_MEDIA_DIRECTORY,
'url' => $mediaUrl.$this->mevHelper::MEV_MEDIA_DIRECTORY.$data[$id]['image']
]
);
$data[$id]['image'] = array($this->image);