Question

I have created a custom module with grid. Now trying to add the product thumbnails to this grid, but the $val in renderer file is empty and images don't show up (see image below).

[![custom module grid images don't show up][1]][1]

Here is the renderer file at ....\Block\Adminhtml\Template\Grid\Renderer\Image.php

class Cpstest_ProductComment_Block_Adminhtml_Template_Grid_Renderer_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
        return $this->_getValue($row);
    }

    protected function _getValue(Varien_Object $row)
    {
        $val = $row->getData($this->getColumn()->getIndex());
        $val = str_replace("no_selection", "", $val);
        $url = Mage::getBaseUrl('media') . 'catalog/product/' . $val;
        $out = "<img src=". $url ." width='60px'/>";
        return $out;
    }
}

And the column code in Grid.php

$this->addColumn('image', array(
    'header'   => Mage::helper('catalog')->__('Image'),
    'align'    => 'left',
    'index'    => 'image',
    'width'    => '70',
    'renderer' => 'Cpstest_ProductComment_Block_Adminhtml_Template_Grid_Renderer_Image'
));

Thanks for any suggestions.

Was it helpful?

Solution

The image attribute isn't part of your custom collection. The easiest way to display the thumbnails is changing your render function to something like below, which loads a product based on productcomment_increment_id and get its thumnail.

public function render(Varien_Object $row)
{
    $product = Mage::getModel('catalog/product')->load($row->getProductcommentIncrementId());
    $url = Mage::getBaseUrl('media') . 'catalog/product/' . $product->getThumbnail();
    $out = "<img src=". $url ." width='60px'/>"; 
    return $out;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top