6

How can i remove 'Delete Image' options from image uploder in system configuration

enter image description here

<field id="image_path" translate="label" type="image" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
 <label>Select Image</label>
 <backend_model>Demo\Generalconfiguration\Model\Config\Backend\Image</backend_model>
 <base_url type="media" scope_info="1">images</base_url>
</field>
0

2 Answers 2

11

You should create a custom renderer for the image. and created a class in your module

[Namespace]_[Module]_Block_Adminhtml_Helper_Image_Required

with this content

class Required extends \Magento\Framework\Data\Form\Element\Image
{
    protected function _getDeleteCheckbox()
    {
        return '';
    }
}

Then in your form block, right above field added this lines

$fieldset->addType('required_image', 'Namespace\Module\Block\Adminhtml\Helper\Image\Required');

and defined field like this:

    $fieldset->addField(
    'image',
    'required_image',               [
    'name' => 'image',
    'label' => __('Image'),
    'id' => 'image',
    'title' => __('Image'),
    'class' => 'required-entry',
    'required' => true,
    ]
);

I hope this will help

1
  • can we remove this with passing argument ?
    – Hiren
    Feb 7, 2019 at 6:36
4

With the help of Muhammad Hasham's answer, I have manage to remove delete checkbox from system config

Image.php

<?php

namespace Demo\Generalconfiguration\Data\Form\Element;

use Magento\Framework\UrlInterface;

class Image extends \Magento\Framework\Data\Form\Element\Image
{
   public function getElementHtml()
   {
    $html = '';

    if ((string)$this->getValue()) {
        $url = $this->_getUrl();

        if (!preg_match("/^http\:\/\/|https\:\/\//", $url)) {
            $url = $this->_urlBuilder->getBaseUrl(['_type' => UrlInterface::URL_TYPE_MEDIA]) .'images/'. $url;
        }

        $html = '<a href="' .
            $url .
            '"' .
            ' onclick="imagePreview(\'' .
            $this->getHtmlId() .
            '_image\'); return false;" ' .
            $this->_getUiId(
                'link'
            ) .
            '>' .
            '<img src="' .
            $url .
            '" id="' .
            $this->getHtmlId() .
            '_image" title="' .
            $this->getValue() .
            '"' .
            ' alt="' .
            $this->getValue() .
            '" height="22" width="22" class="small-image-preview v-middle"  ' .
            $this->_getUiId() .
            ' />' .
            '</a> ';
    }
    $this->setClass('input-file');
    $html .= parent::getElementHtml();

    return $html;
}
}

System.xml

<field id="image_path" translate="label" type="Demo\Generalconfiguration\Data\Form\Element\Image" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Select Image</label>
<backend_model>Magento\Config\Model\Config\Backend\Image</backend_model>
<upload_dir config="system/filesystem/media" scope_info="1">images</upload_dir>
<base_url type="media" scope_info="1">images</base_url>
</field>

Your Answer

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

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