1

This question already has an answer here:

I use this blog

enter image description here

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="actions">
      <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Actions</item>
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="sortOrder" xsi:type="number">30</item>
            </item>
        </argument>
        <field name="simple_action">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="component" xsi:type="string">Cm_Sp/js/form/element/applyoption</item>
                    <item name="label" xsi:type="string" translate="true">Apply</item>
                    <item name="dataType" xsi:type="string">number</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="source" xsi:type="string">sales_rule</item>
                    <item name="dataScope" xsi:type="string">simple_action</item>
                </item>
            </argument>
        </field>

</fieldset>
<fieldset name="highlight_promotion">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Highlight Promotion</item>
            <item name="collapsible" xsi:type="boolean">true</item>
            <item name="sortOrder" xsi:type="number">50</item>
        </item>
    </argument>
    <field name="sp_image">
        <argument name="class" xsi:type="string">Cm\Sp\Model\SalesRule\DataProvider</argument>
            <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">helloworld</item>
                    <item name="label" xsi:type="string" translate="true">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">Cm_Sp/image-preview</item>
                    <item name="required" xsi:type="boolean">false</item>
                    <item name="uploaderConfig" xsi:type="array">
                        <item name="url" xsi:type="url" path="cm_sp/index/upload"/>
                    </item>
                </item>
            </argument>
   </field>
  </fieldset>
</form>

Cm\Sp\etc\di.xml

  <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<virtualType name="Cm\Sp\ImageUpload" type="Cm\Sp\Model\ImageUploader">
    <arguments>
            <argument name="baseTmpPath" xsi:type="string">test/tmp</argument>
            <argument name="basePath" xsi:type="string">test</argument>
            <argument name="allowedExtensions" xsi:type="array">
                <item name="jpg" xsi:type="string">jpg</item>
                <item name="jpeg" xsi:type="string">jpeg</item>
                <item name="gif" xsi:type="string">gif</item>
                <item name="png" xsi:type="string">png</item>
            </argument>
    </arguments>
</virtualType>
<type name="Cm\Sp\Controller\Adminhtml\Index\Upload">
    <arguments>
            <argument name="imageUploader" xsi:type="object">Cm\Sp\ImageUpload</argument>
    </arguments>
</type>
<type name="Cm\Sp\Model\SalesRule\DataProvider">
    <arguments>
        <argument name="imageUploader" xsi:type="object">Cm\Sp\ImageUpload</argument>
    </arguments>
</type>
<type name="Cm\Sp\Observer\SaveImage">
    <arguments>
        <argument name="imageUploader" xsi:type="object">Cm\Sp\ImageUpload</argument>
    </arguments>
</type>
</config>

Cm\Sp\Observer\SaveImage

 <?php
namespace Cm\Sp\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Filesystem\DirectoryList;

class SaveImage implements ObserverInterface
{

    public function execute(Observer $observer)
    {
     // $data = $observer->getRequest()->getPostValue();
      $request = $observer->getEvent()->getRequest();
       $img_name = $request->getPostValue('sp_image');

       if(!empty( $img_name ))
       {
         if(!is_array($img_name ))
         {
            $img = $img_name;  
         }
         else
         {
           $img = $img_name[0]['name']; 
         }
        // $this->imageUploader = \Magento\Framework\App\ObjectManager::getInstance()->get('Cm\Sp\ImageUpload');
        // $this->imageUploader->moveFileFromTmp($img);
       }
       else  
       {
           $img =  NULL;
       }   
      $request->setPostValue('sp_image',$img);
    }  
}

after save the uploaded image and edit the save image field not appear in admin

enter image description here

Cm\Sp\Model\SalesRul\DataProvider.php

namespace Cm\Sp\Model\SalesRule;

use Magento\Store\Model\StoreManagerInterface;

class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{

    protected $salesRuleFactory;
    public function __construct(

    \Cm\Sp\Model\ResourceModel\SalesRule\CollectionFactory $salesRuleFactory, 

    )
    {

    $this->collection = $salesRuleFactory;

    }
    public function getData()
    {

        $items = $this->collection->getItems();

        //Replace icon with fileuploader field name
        foreach ($items as $model) {
            $this->loadedData[$model->getId()] = $model->getData();
            if ($model->getSpImage()) {
                $m['sp_image'][0]['name'] = $model->getSpImage();
                $m['sp_image'][0]['url'] = $this->getMediaUrl().$model->getSpImage();
                $fullData = $this->loadedData;
                $this->loadedData[$model->getId()] = array_merge($fullData[$model->getId()], $m);
            }
        }

        return $this->loadedData;
    }

    public function getMediaUrl()
    {
        $mediaUrl = $this->storeManager->getStore()
            ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).'test/tmp/';
        return $mediaUrl;
    }
}

marked as duplicate by Community Sep 20 at 11:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

Update your getData function in your DataProvider class with this

public function getData()
        {
            $baseurl =  $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

            if (isset($this->loadedData)) {
                return $this->loadedData;
            }
            $items = $this->collection->getItems();
            /** @var \Magento\Cms\Model\Block $block */
            foreach ($items as $block) {
               $temp =  $this->loadedData[$block->getId()] = $block->getData();
                if($temp['image']):

                    $img = [];
                    $img[0]['image'] = $temp['image'];
                    $img[0]['url'] = $baseurl.'test/'.$temp['image'];
                    $temp['image'] = $img;
                    //echo"image found"; exit;

                endif;
            }


            return $this->loadedData;
        }

Hope it works

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