Magento 2 : Remove Delete Image checkbox from image type system configuration
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
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>