eCommerce Software Magento – Shwoing Image in Grid

In Magento on July 30th, 2009

As I said in my last post, Magento Code is awesome. Awesome that drive to hell if you don’t know, awesome like honey once you start knowing. Today I am going to write how to show images in grid by overriding Mage Core Grid Column Class. If you do not know about extension development go here, these guys covered pretty much about extension development. I have copied idea from them ;-) .

Make sure you have following file structure. Only portion inside red-rectangle is applicable for images. This is to override Mage Core Grid Column Class.
Magento Extension Structure to show image in grid
I assume ‘krish’ as namespace and ‘timg’ as extension (module) name

We also need to add option in config.xml (located at krish/timg/etc), which is as follows:





Krish_Timg_Block_Widget_Grid_Column



Showing above is part of code, which means that we are going to override Widget_Grid_Column class. Full config.xml looks like below:

 



0.2.0





Krish_Timg_Model
timg_mysql4


Krish_Timg_Model_Mysql4


timg
Krish_Timg_Block Krish_Timg_Helper Krish_Timg_Block_Widget_Grid_Column Krish_Timg core_setup core_write core_read
101 timg/admin standard Krish_Timg timg

Now, let’s see how we override Widget_Grid_Column. We have to keep those files inside Block folder (located at krish/timg/block). See Folder structure above, inside red rectangle.

Add Column.php file in krish/timg/block/widget/grid, which looks below:


class Krish_Timg_Block_Widget_Grid_Column extends Mage_Adminhtml_Block_Widget_Grid_Column
{
protected function _getRendererByType()
{
switch (strtolower($this->getType())) {
case 'image':
$rendererClass = 'timg/widget_grid_column_renderer_image';
break;
default:
$rendererClass = parent::_getRendererByType();
break;
}
return $rendererClass;
}
protected function _getFilterByType()
{
switch (strtolower($this->getType())) {
case 'image':
$filterClass = 'timg/widget_grid_column_filter_image';
break;
default:
$filterClass = parent::_getFilterByType();
break;
}
return $filterClass;
}
}

If column type is image, it call local class (not Core) from our renderer. Let’s define local renderers. Add Image.php file in krish/timg/block/widget/grid/column/renderer:


class Krish_Timg_Block_Widget_Grid_Column_Renderer_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function __construct() {
}
public function render(Varien_Object $row)
{
return $this->_getValue($row);
}
protected function _getValue(Varien_Object $row)
{
$dored = false;
if ($getter = $this->getColumn()->getGetter()) {
$val = $row->$getter();
}
$val = $val2 = $row->getData($this->getColumn()->getIndex());
$url = Mage::helper('timg')->getImageUrl($val);
$size = Mage::helper('timg')->getImageThumbSize($val);
$file_extis = Mage::helper('timg')->getFileExists($val);
if($file_extis)
$out = " ";
return $out;
}
}

With help of krish_timg helper, it will display image. Let’s define action, this is also located at krish/timg/block/widget/grid/column/renderer. It is really not used so far, I’ll come to it later:

 class Krish_Timg_Block_Widget_Grid_Column_Renderer_Action extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Action
{
protected function _transformActionData(&$action, &$actionCaption, Varien_Object $row)
{
foreach ( $action as $attibute => $value ) {
if(isset($action[$attibute]) && !is_array($action[$attibute])) {
$this->getColumn()->setFormat($action[$attibute]);
$action[$attibute] = parent::render($row);
} else {
$this->getColumn()->setFormat(null);
}
switch ($attibute) {
case 'caption':
$actionCaption = $action['caption'];
unset($action['caption']);
break;
case 'url':
if(is_array($action['url'])) {
$params = array($action['field']=>$this->_getValue($row));
if(isset($action['url']['params'])) {
$params = array_merge($action['url']['params'], $params);
}
$action['href'] = $this->getUrl($action['url']['base'], $params);
unset($action['field']);
} else {
$action['href'] = $action['url'];
}
unset($action['url']);
break;
case 'popup':
$action['onclick'] = 'popWin(this.href, \'windth=800,height=700,resizable=1,scrollbars=1\');return false;';
break;
}
}
return $this;
}
}

Now, let’s define filter which is located at krish/timg/block/widget/grid/column/filter. We just need to inherit it:

 class Krish_Timg_Block_Widget_Grid_Column_Filter_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Filter_Text
{
}

Now, we have to write couple of function in helper which is located at krish/timg/helper:


class Krish_Timg_Helper_Data extends Mage_Core_Helper_Abstract
{
protected static $timgImgDir = null;
protected static $timgImgURL = null;
protected static $timgImgThumb = null;
protected static $timgImgThumbWidth = null;
protected $_allowedExtensions = Array();
public function KrishTimg_Helper_Data()
{
self::$timgImgDir = Mage::getBaseDir('media') .'/krish/timg/';
self::$timgImgURL = Mage::getBaseUrl('media').'/krish/timg/';
self::$timgImgThumb = "thumb/";
self::$timgImgThumbWidth = 100;
}
public function getImageUrl($image_file)
{
$url = false;
if(file_exists(self::$timgImgDir . self::$timgImgThumb . $image_file))
$url = self::$timgImgURL . self::$timgImgThumb . $image_file;
else
$url = self::$timgImgURL . $image_file;
return $url;
}
public function getFileExists($image_file)
{
$file_exists = false;
$file_exists = file_exists(self::$timgImgDir . $image_file);
return $file_exists;
}
public function getImageThumbSize($image_file)
{
$img_file = self::$timgImgDir . $image_file;
if(!file_exists($img_file)) return false;
list($width, $height, $type, $attr) = getimagesize($img_file);
$a_height = (int) ((self::$timgImgThumbWidth / $width) * $height);
return Array('width'=>self::$timgImgThumbWidth, 'height'=>$a_height);
}
function deleteFiles($image_file)
{
$pass = true;
if(!unlink(self::$timgImgDir . $image_file))
$pass = false;
if(!unlink(self::$timgImgDir . self::$timgImgThumb . $image_file))
$pass = false;
return $pass;
}
}

When ever you want to show image, you have to write following code in Grid.php (or wherever if grid code is located)

 $this->addColumn('file_name', array(
'header'        => Mage::helper('timg')->__('Name'),
'align'         => 'left',
'index'         => 'file_name',
'type'         => 'image',
'escape'        => true,
'sortable'  => false
));

That’s all, now you should be able to display image in grid.

Assumptions:

  1. Krish is namespace
  2. timg is module name (sometime I called it extension)
  3. /media/krish/timg is image location and media/krish/timg/thumb is thumbnail location.

Sorry folks for messy code. I try to find better Code Escape plugin for WordPress. I tried with codeautoescape but it doesn’t work proper way. If you know better one, suggest me. Also some of code must have been modified by WordPress editor. I’ll provide full download in next post, including front-end part.

  • Pingback: Learning Magento « Krishna Sunuwar

  • Pingback: Google Realtime Search Result « Krishna Sunuwar

  • Hiral Vyas

    Hi,

    I tried the same code but it is not working.

    Please help

    Thanks

  • Hiral Vyas

    Hi,

    I tried the same code but it is not working.

    Please help

    Thanks

  • Krish

    Can you post what is ur error? What you been able to make so far? What did not work?

  • Krish

    Can you post what is ur error? What you been able to make so far? What did not work?

  • Hiral Vyas

    I have created new module News and what to display image in grid in listing page of Admin > News.

    Now as per the your code, I have created filed…Below is my directory structure for Admin > News

    Code
    –local
    =====================================
    —Gateway
    —-News
    ——Block
    ——–News.php
    ——–Adminhtml
    ———-News.php
    ———-News
    ———–Block
    ————-Widget
    —————Grid
    —————–Column
    ——————-Filer
    ———————Image.php
    ——————-Renderer
    ———————Image.php
    ——————-Column.php
    —————–Column.php
    —————Grid.php
    ———–Grid.php
    =======================================
    ——controllers
    ——etc
    ——Helper
    ——Model
    ——sql

    And place the given code in related files. But still I am not getting image in grid.

  • Hiral Vyas

    I have created new module News and what to display image in grid in listing page of Admin > News.

    Now as per the your code, I have created filed…Below is my directory structure for Admin > News

    Code
    –local
    =====================================
    —Gateway
    —-News
    ——Block
    ——–News.php
    ——–Adminhtml
    ———-News.php
    ———-News
    ———–Block
    ————-Widget
    —————Grid
    —————–Column
    ——————-Filer
    ———————Image.php
    ——————-Renderer
    ———————Image.php
    ——————-Column.php
    —————–Column.php
    —————Grid.php
    ———–Grid.php
    =======================================
    ——controllers
    ——etc
    ——Helper
    ——Model
    ——sql

    And place the given code in related files. But still I am not getting image in grid.

  • Krish

    Do you have other module which shows images in admin grid. Actually, here we are overriding core Grid class, so image path can be conflict

    Can you check HTML source where image should appear. What it appear?

  • Krish

    Do you have other module which shows images in admin grid. Actually, here we are overriding core Grid class, so image path can be conflict

    Can you check HTML source where image should appear. What it appear?

  • Hiral Vyas

    No I did not created any other module which shows image in grid.

    I check HTML source code of News listing page and it is NOT displaying tag it self in between two tags. So obviously it will not showing image.

    I have added below code in my config.xml file too.

    Gateway_News_Block_Widget_Grid_Column

    Thanks

  • Hiral Vyas

    No I did not created any other module which shows image in grid.

    I check HTML source code of News listing page and it is NOT displaying tag it self in between two tags. So obviously it will not showing image.

    I have added below code in my config.xml file too.

    Gateway_News_Block_Widget_Grid_Column

    Thanks

  • Hiral Vyas

    No I did not created any other module which shows image in grid.

    I check HTML source code of News listing page and it is NOT displaying image tag it self in between two td tags. So obviously it will not showing image.

    I have added below code in my config.xml file too.

    

  • Hiral Vyas

    No I did not created any other module which shows image in grid.

    I check HTML source code of News listing page and it is NOT displaying image tag it self in between two td tags. So obviously it will not showing image.

    I have added below code in my config.xml file too.

    

  • Bhavin Thakar

    Hi Krish,

    It you’ve posted a great piece of code and it really helped me out!!

    I just extended the above code in two of my magento custom modules lets say mod1 and mod2. But mod2′s grid listing always considers the block of mod1′s grid widget.
    say for example: mod2 always goes to
    Myspace_Mod1_Block_Widget_Grid_Column

    and not :
    Myspace_Mod2_Block_Widget_Grid_Column

    I’ve check with all my mod2 code and config files but unable to trace out the reason.

    Any ideas??

    Thanks in advance.
    Bhavin

  • Bhavin Thakar

    Hi Krish,

    It you’ve posted a great piece of code and it really helped me out!!

    I just extended the above code in two of my magento custom modules lets say mod1 and mod2. But mod2′s grid listing always considers the block of mod1′s grid widget.
    say for example: mod2 always goes to
    Myspace_Mod1_Block_Widget_Grid_Column

    and not :
    Myspace_Mod2_Block_Widget_Grid_Column

    I’ve check with all my mod2 code and config files but unable to trace out the reason.

    Any ideas??

    Thanks in advance.
    Bhavin

  • Bhavin Thakar

    Hi Krish,

    You’ve posted a great piece of code and it really helped me out!!

    I just extended the above code in two of my magento custom modules lets say mod1 and mod2. But mod2’s grid listing always considers the block of mod1’s grid widget.
    say for example: mod2 always goes to
    Myspace_Mod1_Block_Widget_Grid_Column

    and not :
    Myspace_Mod2_Block_Widget_Grid_Column

    I’ve check with all my mod2 code and config files but unable to trace out the reason.

    Any ideas??

  • Bhavin Thakar

    Hi Krish,

    You’ve posted a great piece of code and it really helped me out!!

    I just extended the above code in two of my magento custom modules lets say mod1 and mod2. But mod2’s grid listing always considers the block of mod1’s grid widget.
    say for example: mod2 always goes to
    Myspace_Mod1_Block_Widget_Grid_Column

    and not :
    Myspace_Mod2_Block_Widget_Grid_Column

    I’ve check with all my mod2 code and config files but unable to trace out the reason.

    Any ideas??

  • Krish

    Hi Bhavin!

    Sorry for the late reply.

    Well, I have experience that problem myself too. This is because Magento Framework load all modules once and one module easily can override another module when you overload.

    Here’s how you can solve this problem:
    1) Have same file path for both Module
    2) Instead of defining file path variable in helper, define in controller.

    I hope, this helps you.

  • Krish

    Hi Bhavin!

    Sorry for the late reply.

    Well, I have experience that problem myself too. This is because Magento Framework load all modules once and one module easily can override another module when you overload.

    Here’s how you can solve this problem:
    1) Have same file path for both Module
    2) Instead of defining file path variable in helper, define in controller.

    I hope, this helps you.