Tony's Blog

Magento 2 image not uploading in edit form

  • DataProvider of UI Component:
  1. <argument name="dataProvider" xsi:type="configurableObject">
  2. <argument name="class" xsi:type="string">Loyalty\PointsMall\Model\ResourceModel\Products\DataProvider</argument>
  3. <argument name="name" xsi:type="string">loyalty_pointsMall_products_edit_data_source</argument>
  4. <argument name="primaryFieldName" xsi:type="string">earning_rule_id</argument>
  5. <argument name="requestFieldName" xsi:type="string">id</argument>
  6. <argument name="data" xsi:type="array">
  7. <item name="config" xsi:type="array">
  8. <item name="submit_url" xsi:type="url" path="*/*/save"/>
  9. </item>
  10. </argument>
  11. </argument>
  • DataProvider.php
  1. public function getData()
  2. {
  3. if($this->request->getParams('id')){
  4. $collcetion = $this->productsFactory->create();
  5. $collcetion->load($this->request->getParams('id'));
  6. $this->_loadedData[$collcetion->getId()] = $collcetion->getData();
  7. }
  8. return $this->_loadedData;
  9. }
  • Change function getData() as follow:
  1. public function getData()
  2. {
  3. if($this->request->getParams('id')){
  4. $collcetion = $this->productsFactory->create();
  5. $collcetion->load($this->request->getParams('id'));
  6. $productData = $collcetion->getData();
  7. if (isset($productData['image'])) {
  8. unset($productData['image']);
  9. $productData['image'][0]['name'] = $collcetion->getData('image');
  10. $productData['image'][0]['url'] = $this->imageHelper->getBaseUrl().$collcetion->getData('image');
  11. }
  12. $this->_loadedData[$collcetion->getId()] = $productData;
  13. }
  14. return $this->_loadedData;
  15. }

After do that, it will be work.

Magento 2 image not uploading in edit form