2

I have created a Grid and Edit form for admin. I can view other elements like textbox and selectbox in form. But now i need to show a preview of the uploaded image to the admin so that admin view it and approve the image. I don't want to add a image uploader just need to show a thumbnail preview of the image and on clicking that image it should open it in a popup window.

Here is the code i have used for the form.

class Buildnext_producteditrequest_Block_Adminhtml_Producteditrequest_Edit_Tab_Form 
      extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form();
        $this->setForm($form);
        $fieldset = $form->addFieldset('producteditrequest_form', array('legend'=>Mage::helper('producteditrequest')->__('Edit Request  ')));

        $productEditID = $this->getRequest()
                          ->getParam('id');

        $pdtEditDetails = Mage::getModel('producteditrequest/producteditrequest')->load($productEditID);        
        $sellerDetails = Mage::getModel('customer/customer')->load($pdtEditDetails['seller_id']);
        $proudctDetails = Mage::getModel('catalog/product')->load($pdtEditDetails['product_id']);

        $roomBudget = array();
        $productCollection = Mage::getResourceModel('catalog/product_collection');

        $productCollection->addAttributeToSelect('room_budget')
        ->addAttributeToSort('entity_id','ASC')
        ->groupByAttribute('room_budget');

        foreach($productCollection as $collection){
            $strRoomBudget = $collection->getAttributeText('room_budget');
            if($strRoomBudget){
                array_push($roomBudget,array('value'=>$collection->getRoomBudget(),
                                              'label'=>$strRoomBudget));
            }
        }

        $fieldset->addField('productcode', 'text', array(
            'label'     => Mage::helper('producteditrequest')->__('Product Code'),
            'name'      => 'productCode',
             'readonly' => true,
            'value' => $pdtEditDetails['product_code'], 
        ));

        $fieldset->addField('productname', 'text', array(
                'label'     => Mage::helper('producteditrequest')->__('Product'),
                'required'  => false,
                'readonly' => true,
                'value' => $proudctDetails->getName(),
        ));

        $fieldset->addField('seller', 'text', array(
                'label'     => Mage::helper('producteditrequest')->__('Seller'),
                'required'  => false,
                'readonly' => true,
                'value' => $sellerDetails->getName(),
        ));

        $fieldset->addField('productbudget', 'select', array(
            'label'     => Mage::helper('producteditrequest')->__('Product Budget'),
            'name'      => 'productbudget',
            'values'    => $roomBudget,
            'value'     => $pdtEditDetails['product_budget'],
        ));

        $fieldset->addField('shortdesc', 'editor', array(
            'name'      => 'shortdesc',
            'label'     => Mage::helper('producteditrequest')->__('Short Description'),
            'title'     => Mage::helper('producteditrequest')->__('Short Description'),
            'style'     => 'width:98%; height:200px;',
            'wysiwyg'   => false,
            'required'  => true,
            'value' => $pdtEditDetails['short_desc']
        ));

        $fieldset->addField('desc', 'editor', array(
                'name'      => 'description',
                'label'     => Mage::helper('producteditrequest')->__('Description'),
                'title'     => Mage::helper('producteditrequest')->__('Description'),
                'style'     => 'width:98%; height:200px;',
                'wysiwyg'   => false,
                'required'  => true,
                'value' => $pdtEditDetails['description']
        ));

        $fieldset->addField('length_after_packing', 'text', array(
                'label'     => Mage::helper('producteditrequest')->__('Length After Packing'),
                'required'  => false,
                'readonly' => true,
                'name' => 'length_after_packing',
                'value' => $pdtEditDetails['length_after_packing'],
        ));

        $fieldset->addField('width_after_packing', 'text', array(
                'label'     => Mage::helper('producteditrequest')->__('Width After Packing'),
                'required'  => false,
                'readonly' => true,
                'name' => 'width_after_packing',
                'value' => $pdtEditDetails['width_after_packing'],
        ));

        $fieldset->addField('height_after_packing', 'text', array(
                'label'     => Mage::helper('producteditrequest')->__('Height After Packing'),
                'required'  => false,
                'readonly' => true,
                'name' => 'height_after_packing',
                'value' => $pdtEditDetails['height_after_packing'],
        ));

        $fieldset->addField('height_without_packing', 'text', array(
                'label'     => Mage::helper('producteditrequest')->__('Height Without Packing'),
                'required'  => false,
                'readonly' => true,
                'name' => 'height_without_packing',
                'value' => $pdtEditDetails['height_without_packing'],
        ));

        $fieldset->addField('length_without_packing', 'text', array(
                'label'     => Mage::helper('producteditrequest')->__('Length Without Packing'),
                'required'  => false,
                'readonly' => true,
                'name' => 'length_without_packing',
                'value' => $pdtEditDetails['length_without_packing'],
        ));

        $fieldset->addField('width_without_packing', 'text', array(
                'label'     => Mage::helper('producteditrequest')->__('Width Without Packing'),
                'required'  => false,
                'readonly' => true,
                'name' => 'width_without_packing',
                'value' => $pdtEditDetails['width_without_packing'],
        ));

        $fieldset->addField('quality_certificate', 'text', array(
                'label'     => Mage::helper('producteditrequest')->__('Width Without Packing'),
                'required'  => false,
                'readonly' => true,
                'name' => 'width_without_packing',
                'value' => $pdtEditDetails['width_without_packing'],
        ));

        /* print_r(Mage::getSingleton('adminhtml/session')->getProducteditrequestData());

        if ( Mage::getSingleton('adminhtml/session')->getProducteditrequestData() )
        {
            $form->setValues(Mage::getSingleton('adminhtml/session')->getProducteditrequestData());
            Mage::getSingleton('adminhtml/session')->setProducteditrequestData(null);
        } elseif ( Mage::registry('producteditrequest_data') ) {
            $form->setValues(Mage::registry('producteditrequest_data')->getData());
        } */
        return parent::_prepareForm();
    }
}

Please help.

3

You can show the image as below:

Step 1 : Add Type

$fieldset->addType('custom', 'Buildnext_producteditrequest_Block_Adminhtml_Form_Element_File');

Step 2: Add Image View Code

$fieldset->addField('product_image1', 'custom', array(
            'name'      => 'product_image1',
            'label'     => Mage::helper('Buildnext_producteditrequest')->__('Image1'),
)); //product_image1 column name image

Step 3: Change get Element Code in Buildnext_producteditrequest_Block_Adminhtml_Form_Element_File

public function getElementHtml()
{
    $html = '';
    if ((string)$this->getValue()) {
        $url = $this->_getUrl();

        if( !preg_match("/^http\:\/\/|https\:\/\//", $url) ) {
            $url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB).'upload'.DS. $url; // path of image folder
        }

        $html = "<img width='100px' height='100px' src='".$url."' />"; // image html
    }else{ // image not uploaded show default image
        $url = 'no-image.jpg';
        $url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB).'upload'.DS. $url;
        $html = "<img width='100px' height='100px' src='".$url."' />";
    }
    return $html;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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