Question

How to change image path of a Field of type "image" ?

In my custom module adminhtml form I am using field as type "image" to show thum image. I want to change the default path of this thum and want to use my custom path here, as my image upload path is different.

Here is my addField example :

$fieldset->addField(
        'image',
        'image',
        array(
            'name' => 'image',
            'label' => __('Example Image'),
            'title' => __('Example Image'),
            'required' => false,
        )
    );
Was it helpful?

Solution

You can use custom type and extend magento image type

change your code in your form.php as below

$fieldset->addType('custom_image','VendorName\ModuleName\Helper\Image\Custom');
$fieldset->addField(
    'image',
    'custom_image',
    array(
        'name' => 'image',
        'label' => __('Your Lable'),
        'title' => __('Your Title'),
        'required' => false,
    )
);

Create that Block class and extend \Magento\Framework\Data\Form\Element\Image

namespace VendorName\ModuleName\Helper\Image;

class Custom extends \Magento\Framework\Data\Form\Element\Image
{
    public function _getUrl()
    {   
        $url = 'YourModulePath'.$this->getValue(); 
        return $url;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top