2

I'm trying to display product thumbnails in my custom module admin form. But for some reason no thumbnail is showing up.

When I use the code below, it only shows the browse button with no thumbnail, like in the image below.

protected function _prepareForm()
{
$model = Mage::registry('cpstest_productcomment'); 
$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->addField('image', 'image', array(
'label'     => Mage::helper('catalog')->__('Image'),
'required'  => false,
'name'     => 'image',              
));
}

And when I use this code, I get an error:
Fatal error: Call to a member function getData() on null in ...\app\code\core\Mage\Catalog\Helper\Image.php on line

$fieldset->addField('image', 'image', array(
'label'     => Mage::helper('catalog')->__('Image'),
'required'  => false, // Make true if required field
'name'     => 'image',
'value' => Mage::helper('catalog/image')->init($product, 'thumbnail')->resize(100)              
));

enter image description here

2
  • provide full code of this file Commented Jun 2, 2017 at 10:31
  • Hi @Murtuza, I added more details to the question. Commented Jun 2, 2017 at 18:26

2 Answers 2

0

For showing image into edit form, you want to render the image.

Replace your addField to below

$fieldset->addField('image', 'image', array(
    'label'    => Mage::helper('catalog')->__('Image'),
    'required' => false, // Make true if required field
    'name'     => 'image',
    'renderer' => 'employee/adminhtml_employee_renderer_image',         
));

Here as you can see we have specified an renderer block type as employee/adminhtml_employee_renderer_image. Now we need to create this Block. The code inside this block is as follows:

<?php
class Namespace_Employee_Block_Adminhtml_Employee_Renderer_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
        $imageName = $row->getAfterImage(); 
        $imagePath = Mage::getBaseUrl("media").$imageName;                 

        $io = new Varien_Io_File();
        if ($io->fileExists($imagePath) && !empty($imageName)) {           
            return "<img width='50px' height='50px' src='".$imagePath."' />"; 
        } else {
            return ""; 
        }
    }
}
5
  • Hi @Arvind, this solution didn't work unfortunately. It shows the same browse button with no image like in the print screen in my question. Commented Jun 2, 2017 at 18:14
  • Hello @bestwebdevs Please, Provide me full code of file and save action of controller. Commented Jun 5, 2017 at 10:56
  • Hi @ARVIND, I added those codes, hope you will be able to help me. Commented Jun 5, 2017 at 17:21
  • You see the path of image shown in form? Just check image path it may be incorrect. Commented Jun 6, 2017 at 9:13
  • Hi @Arvind, the path is correct. Images are being uploaded correctly, but I have no clue why they don't show up in the form. Commented Jun 6, 2017 at 16:49
0

This is how I resolved this issue:

I changed the structure of my database table and added a new column named product_image_url and I saved the URLs of product images there. After I simply used that column information and added it to my admin form like this (without using catalog/image helper):

$fieldset->addField('product_image_url', 'image', array(
'name'   => 'product_image_url',            
'label'     => Mage::helper('adminhtml')->__('Product Image'),
'title'     => Mage::helper('adminhtml')->__('Product Image'),
'required'  => false
)); 

enter image description here

I was not able to make the image bigger, but you can expand it by clicking on it.

Hope this will help someone.

Your Answer

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

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