0
    <field name="image_upload" sortOrder="40" formElement="imageUploader">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="source" xsi:type="string">source</item>
            </item>
        </argument>
        <settings>
            <elementTmpl>ui/form/element/uploader/image</elementTmpl>
            <dataType>string</dataType>
            <label translate="true">Topic Image</label>
            <visible>true</visible>
            <required>false</required>
        </settings>
        <formElements>
            <imageUploader>
                <settings>
                    <required>false</required>
                    <uploaderConfig>
                        <param xsi:type="url" name="url" path="path/to/upload"/>
                    </uploaderConfig>
                    <previewTmpl>My_Module/image-preview</previewTmpl>
                    <openDialogTitle>Media Gallery</openDialogTitle>
                    <allowedExtensions>jpg jpeg gif png</allowedExtensions>
                    <maxFileSize>4194304</maxFileSize>
                </settings>
            </imageUploader>
        </formElements>
    </field>

im using this code on uploading image. then in my save action, im saving the image url path (eg. project.local/image/path). my problem is in the edit form. its not loading the saved image. can someone help me on how i can load the image in the edit form, or maybe an image field that i can use just to load the saved image. note that what im saving in the DB is the url path of the image.

Thank you

DataProvider.php

public function __construct(
    $name,
    $primaryFieldName,
    $requestFieldName,
    \My\Module\Model\ResourceModel\Topics\Collection $topicsCollection,
    \Magento\Framework\App\Request\DataPersistorInterface $dataPersistor,
    \Magento\Framework\App\RequestInterface $request,
    array $meta = [],
    array $data = []
){
    $this->collection = $topicsCollection;
    $this->dataPersistor = $dataPersistor;
    $this->request = $request;
    $this->resourceHelper = $resourceHelper;
    parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
}

public function getData()
{
    if (isset($this->loadedData)) {
        return $this->loadedData;
    }

    $topicId = $this->request->getParam('topic_id');
    if($topicId){
        $topicCollection = $this->collection->addFieldToFilter('topic_id', $topicId);
        foreach ($topicCollection as $topic){
            $this->loadedData[$topic->getId()] = $topic->getData();
        }
    }

    return $this->loadedData;
}
3
  • please check your data provide code. and share your code here also
    – Msquare
    Commented Sep 21, 2020 at 10:54
  • @Msquare updated my question. my dataProvider loadedData should have an 'image_upload' field. and the value is the media url path of the uploaded image
    – mendz
    Commented Sep 21, 2020 at 11:36
  • please check and update me.
    – Msquare
    Commented Sep 21, 2020 at 12:17

1 Answer 1

1

Try this code

protected $storeManager;


public function __construct(
    $name,
    $primaryFieldName,
    $requestFieldName,
    \My\Module\Model\ResourceModel\Topics\Collection $topicsCollection,
    \Magento\Framework\App\Request\DataPersistorInterface $dataPersistor,
    \Magento\Framework\App\RequestInterface $request,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    array $meta = [],
    array $data = []
){
    $this->collection = $topicsCollection;
    $this->dataPersistor = $dataPersistor;
    $this->request = $request;
    $this->resourceHelper = $resourceHelper;
    $this->_storeManager = $storeManager;
    parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
}

public function getData()
{
    if (isset($this->loadedData)) {
        return $this->loadedData;
    }
    $topicId = $this->request->getParam('topic_id');
    if($topicId){
        $topicCollection = $this->collection->addFieldToFilter('topic_id', $topicId);
        foreach ($topicCollection as $topic){
            $this->loadedData[$topic->getId()] = $topic->getData();
            // set your image
            if ($topic->getImageUpload() != '') {
                $label_img['image_upload'][0]['name'] = $topic->getImageUpload();
                $label_img['image_upload'][0]['url'] = $this->getMediaUrl() . $topic->getImageUpload();
                $fullData = $this->loadedData;
                $this->loadedData[$topic->getId()] = array_merge($fullData[$topic->getId()], $label_img);
            }
        }
    }
    return $this->loadedData;
}

public function getMediaUrl()
{
    // set your image path here
    $mediaUrl = $this->_storeManager->getStore()
        ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'image/path/';
    return $mediaUrl;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.