0

I currently have this set up to display my catalog images in a magento custom grid - I seem to be close but cannot seem to call the final image url to display- can anyone help me?

I have tried calling them from /media/remote/cache also but to no avail :-(

public function render(Varien_Object $row) {

    $p = Mage::getModel('catalog/product')->load($row->getproduct_id());
    $html = '<img src="' . Mage::getBaseUrl('media').'catalog/product/'.'" width="50" height="50" alt="' . $p->getname() . '" />';
    return $html;
}

1 Answers1

0

you have to create your custom render like below

just add this to your grid

  $this->addColumn('thumbnail',
                array(
                    'header'=> Mage::helper('catalog')->__('Thumbnail'),
                    'type'  => 'image',
                    'width' => '100',
                    'index' => 'thumbnail',
                    'renderer' => 'NAMESPACE_YOURMODULE_Block_Widget_Grid_Column_Renderer_Image',
            ));

and create render class at

\NAMESPACE\YOURMODULE\Block\Widget\Grid\Column\Renderer\Image.php

and add below code to this files with proper class name

class NAMESPACE_YOURMODULE_Block_Widget_Grid_Column_Renderer_Image 
    extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
protected static $showImagesUrl = null;
protected static $showByDefault = null;

protected static $imagesWidth = null;
protected static $imagesHeight = null;
protected static $imagesScale = null;

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

protected static function initSettings()
{
    if(!self::$showImagesUrl)
        self::$showImagesUrl = (int)Mage::getStoreConfig('yourmodule/images/showurl') === 1;
    if(!self::$showByDefault)
        self::$showByDefault = (int)Mage::getStoreConfig('yourmodule/images/showbydefault') === 1;
    if(!self::$imagesWidth)
        self::$imagesWidth = Mage::getStoreConfig('yourmodule/images/width');
    if(!self::$imagesHeight)
        self::$imagesHeight = Mage::getStoreConfig('yourmodule/images/height');
    if(!self::$imagesScale)
        self::$imagesScale = Mage::getStoreConfig('yourmodule/images/scale');
}



protected function _getValue(Varien_Object $row)
{
    self::initSettings();

    $noSelection    =   false;
    $dored          =   false;
    if ($getter = $this->getColumn()->getGetter())
    {
        $val = $row->$getter();
    }

    $val = $val2 = $row->getData($this->getColumn()->getIndex());
    $noSelection = ($val == 'no_selection' || $val == '') ? true : $noSelection;
    $url = Mage::helper('yourmodule')->getImageUrl($val);

    if(!Mage::helper('yourmodule')->getFileExists($val)) 
    {
      $dored = true;
      $val .= "[*]";
    }

    $dored = (strpos($val, "placeholder/")) ? true : $dored;
    $filename = (!self::$showImagesUrl) ? '' : substr($val2, strrpos($val2, "/")+1, strlen($val2)-strrpos($val2, "/")-1);

    $val = ($dored) ? 
            ("<span style=\"color:red\" id=\"img\">$filename</span>") :
            "<span>". $filename ."</span>";

    $out = (!$noSelection) ? 
            ($val. '<center><a href="#" onclick="window.open(\''. $url .'\', \''. $val2 .'\')" title="'. $val2 .'" '. ' url="'.$url.'" id="imageurl">') :
            '';

    $outImagesWidth = self::$imagesWidth ? "width='".self::$imagesWidth."'":'';
    if(self::$imagesScale)
        $outImagesHeight = (self::$imagesHeight) ? "height='".self::$imagesHeight."'":'';
    else
        $outImagesHeight = (self::$imagesHeight && !self::$imagesWidth) ? "height='".self::$imagesHeight."'":'';

    //return '<img src="'.(Mage::helper("catalog/image")->init($productItem, "small_image")->resize(self::$imagesWidth)).'" '.$outImagesWidth.' '.$outImagesHeight.' alt="" />';




     $out .= (!$noSelection) ? 
                "<img src=".(Mage::helper("catalog/image")->init($row, $this->getColumn()->getIndex())->resize(self::$imagesWidth))." ".$outImagesWidth." ".$outImagesHeight." border=\"0\"/>" :
//                "<img src=".$url." ".$outImagesWidth." ".$outImagesHeight." border=\"0\"/>" :
//                "" :
                "<center><strong>[".__('NO IMAGE')."]</strong></center>";

        return $out. ((!$noSelection)? '</a></center>' : '');
    }

hope this will sure help you.

liyakat
  • 11,825
  • 2
  • 40
  • 46
  • I dont appear to have had any luck with this one - I have used this in the past:load($_item['product_id']); $i=0; foreach but it just isnt working for me :-( Do you have any other ideas? – user3313215 Feb 15 '14 at 12:52
  • you can use like echo Mage::helper('catalog/image')->init($products, 'small_image')->resize(163,100); // resize function is used to resize image if u have product collection object – liyakat Feb 15 '14 at 13:06
  • where $products = Mage::getModel('catalog/product')->load(1); //Product ID – liyakat Feb 15 '14 at 13:06
  • Still no luck what about this? A bit closer? $p = Mage::getModel('catalog/product')->load($row->getproduct_id()); $html = '' . $p->getname() . ''; return $html; – user3313215 Feb 15 '14 at 13:43
  • where $row->getproduct_id() should be from for loop as collection objects ? see this will sure help u http://stackoverflow.com/questions/2474117/how-to-get-a-products-image-in-magento – liyakat Feb 17 '14 at 04:50
  • still not working for me and convinced it is the external / media/remote folder. Does anyone know the handle for this ? – user3313215 Feb 22 '14 at 16:19