0

I am trying to add an Image using my custom admin form. The image is saved while uploading it via custom admin form. But does not displays in the admin grid image column of my custom-created module. Here is the screenshot. enter image description here As you can see my image has been saved using my custom admin form. The src of an image is generated and the image is been displayed in the my custom created form.

enter image description here But the image does not displyed in the admin grid. I had created the renderer to get the image but it doe not work for me. The renderer doesn't generate the src of my image. Please check my code below:

Grid.php

$this->addColumn("image", array(
        "header"    => Mage::helper("news")->__("Image"),
        "type"      => "text",
        "width"     => "150px",
        "index"     => "image",
        "renderer" => "Custom_News_Block_Adminhtml_News_Grid_Renderer_Image",
    ));

Custom/News/Block/Adminhtml/News/Grid/Renderer/Image.php

class Custom_News_Block_Adminhtml_News_Grid_Renderer_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {
public function render(Varien_Object $row) {
    $val = Mage::helper('news/news')->init($row, 'thumbnail')->resize(100);
    $out = '<img src=". $val ." width="100px" height="100px"/>';
    return $out;
}   }

Form.php

$fieldset->addField('image', 'image', array(
        "label"     => Mage::helper("news")->__("Image"),
        "name"      => "image",
        "note" => "(*.jpg, *.png, *.gif)",
      ));

NewsController.php

try {   //Save Image
try {
    if ((bool) $postData['image']['delete'] == 1) {
        $postData['image'] = '';
    } else {
        unset($postData['image']);
        if (isset($_FILES)) {
            if ($_FILES['image']['name']) {
                if ($this->getRequest()->getParam("id")) {
                    $model = Mage::getModel("news/news")->load($this->getRequest()->getParam("id"));
                    if ($model->getData('image')) {
                        $io = new Varien_Io_File();
                        $io->rm(Mage::getBaseDir('media') . DS . implode(DS, explode('/', $model->getData('image'))));
                    }
                }
                $path = Mage::getBaseDir('media') . DS . 'news' . DS . 'news' . DS;
                $uploader = new Varien_File_Uploader('image');
                $uploader->setAllowedExtensions(array('jpg', 'png', 'gif'));
                $uploader->setAllowRenameFiles(true);
                $uploader->setFilesDispersion(false);
                $destFile = $path . $_FILES['image']['name'];
                $fileName = $uploader->getNewFileName($destFile);
                $uploader->save($path, $fileName);
                $postData['image'] = "news/news/".$fileName;
            }
        }
    }
} catch (Exception $e) {
    Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
    $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
    return;
}

Can anyone please help me to get it solved. I had refered this this post to create renderer for my custom grid.

0

After lots of research and debugging my custom code. I found the solution, and it worked fine for me.

I had applied changes in my renderer, and it gave me the image path and now I am able to display an image in my custom grid Custom/News/Block/Adminhtml/News/Grid/Renderer/Image.php

Class Custom_News_Block_Adminhtml_News_Grid_Renderer_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {
public function render(Varien_Object $row) {
    $img = $row->getData($this->getColumn()->getIndex());
    if ($img && strpos($img, 'no_selection') !== 0) {
        return sprintf('<img src="%s" width="60px" />', Mage::getBaseUrl('media').$img);
    }
    return '';
}   }

Please check the below screenshot of my custom grid.

enter image description here Please check and vote for my answer if you feel helpfull.

Thanks.

| improve this answer | |
  • Hii @AshokDubariya thanks for edit. – MagentoUser Nov 22 '19 at 9:23

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.