分类
后端

ui_component form 单图片上传

FME\GoogleMapsStoreLocator\Controller\Adminhtml\Post\Upload\FeaturedImg

<?php
namespace FME\GoogleMapsStoreLocator\Controller\Adminhtml\Post\Upload;

use FME\GoogleMapsStoreLocator\Controller\Adminhtml\Upload\Image\Action;

/**
 * Blog featured image upload controller
 */
class FeaturedImg extends Action
{
    /**
     * File key
     *
     * @var string
     */
    protected $_fileKey = 'signature';

    /**
     * Check admin permissions for this controller
     *
     * @return boolean
     */
    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('FME_GoogleMapsStoreLocator::storelocator_save');
    }
}
view/adminhtml/ui_component/googlemapsstorelocator_storelocator_form.xml

<argument 下面增加
    <dataSource name="storelocator_form_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">FME\GoogleMapsStoreLocator\Model\Storelocator\DataProvider</argument>
            <argument name="name" xsi:type="string">storelocator_form_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">gmaps_id</argument>
            <argument name="requestFieldName" xsi:type="string">gmaps_id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="submit_url" xsi:type="url" path="googlemapsstorelocator/storelocator/save"/>
                </item>
            </argument>
        </argument>
        <argument name="data" xsi:type="array">
            <item name="js_config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/form/provider</item>
            </item>
        </argument>
    </dataSource>

                <field name="signature">
                    <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">storelocator</item>
                            <item name="label" xsi:type="string" translate="true">Signature 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="uploaderConfig" xsi:type="array">
                                <item name="url" xsi:type="url" path="googlemapsstorelocator/post_upload/featuredImg"/>
                            </item>
                            <item name="sortOrder" xsi:type="number">10</item>
                        </item>
                    </argument>
                </field>
FME\GoogleMapsStoreLocator\Model\Storelocator\DataProvider

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace FME\GoogleMapsStoreLocator\Model\Storelocator;

use FME\GoogleMapsStoreLocator\Model\ResourceModel\Storelocator\CollectionFactory;
use Magento\Framework\App\Request\DataPersistorInterface;

/**
 * Class DataProvider
 */
class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{

    protected $collection;

    /**
     * @var DataPersistorInterface
     */
    protected $dataPersistor;
    public $_storeManager;
    /**
     * @var array
     */
    protected $loadedData;

    /**
     * Constructor
     *
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param CollectionFactory $blockCollectionFactory
     * @param DataPersistorInterface $dataPersistor
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $blockCollectionFactory,
        DataPersistorInterface $dataPersistor,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        array $meta = [],
        array $data = []
    ) {
        $this->collection = $blockCollectionFactory->create();
        $this->_storeManager=$storeManager;
        $this->dataPersistor = $dataPersistor;
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
    }

    /**
     * Get data
     *
     * @return array
     */
    public function getData()
    {

        if (isset($this->loadedData)) {
            return $this->loadedData;
        }
        $items = $this->collection->getItems();

        foreach ($items as $page) {

            $itemData = $page->getData();
            if (isset($itemData['signature'])) {
                $info = pathinfo($itemData['signature']);
                $itemData['signature'] = [
                    [
                        'name' => $info['filename'],
                        'url' =>  $itemData['signature'],
                    ],
                ];
            }
            $this->loadedData[$page->getId()] = $itemData;
        }

        $data = $this->dataPersistor->get('fme_googlemapsstorelocator');
        if (!empty($data)) {
            $page = $this->collection->getNewEmptyItem();
            $page->setData($data);
            $this->loadedData[$page->getId()] = $page->getData();
            $this->dataPersistor->clear('fme_googlemapsstorelocator');
        }

        return $this->loadedData;
    }
}