1
<field name="page_image"> 
 <argument name="data" xsi:type="array">
 <item name="config" xsi:type="array">
 <item name="dataType" xsi:type="string">image</item>
 <item name="source" xsi:type="string">page</item>
 <item name="label" xsi:type="string" translate="true">Banner Image</item>
 <item name="visible" xsi:type="boolean">true</item>
 <item name="formElement" xsi:type="string">image</item> 
 <item name="required" xsi:type="boolean">true</item> 
 </item>
 </argument>
</field> 

Image uploaded successfully & saved in database also. But I can not preview the image in the edit screen. How to preview the image with delete checkbox?

asked Sep 15, 2017 at 5:34

1 Answer 1

0

I had the same problem, the fileuploader uses a specific array to preview the image and you have to create it manually.

You have to extend the source defined in:

<item name="source" xsi:type="string">page</item>

And extend the DataProvider in di.xml:

<preference for="Magento\Cms\Model\Page\DataProvider" type="Dexanet\Cms\Model\Page\DataProvider"/>

You have to extend the getData() method like:

public function getData() {
 $data = parent::getData();
 foreach ($data as $pageId => $page) {
 if (isset($page['header_image'])) {
 unset($data[$pageId]['header_image']);
 $fileName = $page['header_image'];
 $serverPath = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)
 ->getAbsolutePath() . $this->imageUploader->getBasePath() . "/" . $fileName;
 $fullPath = $this->storeManager->getStore()->getBaseUrl(
 \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
 ) . $this->imageUploader->getBasePath() . "/" . $fileName;
 if (file_exists($serverPath)) {
 $stat = stat($serverPath);
 $mime = mime_content_type($serverPath);
 $data[$pageId]["header_image"][0]['name'] = $fileName;
 $data[$pageId]["header_image"][0]['url'] = $fullPath;
 $data[$pageId]['header_image'][0]['size'] = isset($stat) ? $stat['size'] : 0;
 $data[$pageId]['header_image'][0]['type'] = $mime;
 }
 }
 }
 return $data;
}

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.