Showing posts with label Thumbnail. Show all posts
Showing posts with label Thumbnail. Show all posts

Tuesday 13 September 2016

Show thumbnail image / file in custom module admin form Magento




Just create one file and extended it with magento core abstract class. See example

Just add code into Your Form.php

class Kanasagra_Jaydip_Block_Adminhtml_{{Entity}}_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form {
    protected function _prepareForm() {
       
        $form = new Varien_Data_Form(array(
            'id' => 'edit_form',
            'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
            'method' => 'post',
            'enctype' => 'multipart/form-data'
                )
        );
        $fieldset = $form->addFieldset('{{Entity}}_form', array('legend' => Mage::helper('{{Entity}}')->__('Product information')));
        $fieldset->addType('file', Mage::getConfig()                                                                                              ->getBlockClassName('jaydip/Adminhtml_{{Entity}}_Helper_File'));        $fieldset->addField('svgimage', 'file', array(            'label' => Mage::helper('jaydip')->__('Upload SVG'),            'required' => false,            'name' => 'svgimage',        ));     ...
     ...
     ...
    }
}

Create New file in Kanasagra/Jaydip/Block/Adminhtml/{{Entity}}/Helper/File.php

<?php
class Kanasagra_Jaydip_Block_Adminhtml_{{Entity}}_Helper_File extends Varien_Data_Form_Element_Abstract{
   
    public function __construct($data){
        parent::__construct($data);
        $this->setType('file');
    }
    public function getElementHtml(){
        $html = '';
        $this->addClass('input-file');
        $html.= parent::getElementHtml();
        if ($this->getValue()) {
            $url = $this->_getUrl();
            if( !preg_match("/^http\:\/\/|https\:\/\//", $url) ) {
                $url = Mage::getBaseUrl('media').'photo_image/images/' . $url; //replace this with the path to the file if you upload it somewhere else
            }
            $html .= '<br /><img width="100px" src="'.$url.'">';
        }
        return $html;
    }
    protected function _getUrl(){
        return $this->getValue();
    }
}