Image Grid Column Renderer

To show product image previews in Magento Backend, you have to add a image grid column renderer block first:

<?php
/**
 *
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * It is  available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 *
 *
 * @category   AskSheldon
 * @package    AskSheldon_Adminhtml
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author     Marcel Lange <info@ask-sheldon.com>
 *
 * Renders the productimages in product grid in BE
 *
 * AskSheldon_Adminhtml_Block_Catalog_Widget_Grid_Column_Renderer_Image
 *
 */
class AskSheldon_Adminhtml_Block_Catalog_Widget_Grid_Column_Renderer_Image 
    extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    /**
     * Renders grid column
     *
     * @param   Varien_Object $row
     * @return  string
     */
    public function render(Varien_Object $row)
    {
        if($row->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE){
            $src = $this->helper('catalog/image')->init($row, $this->getColumn()->getIndex())->resize((($this->getColumn()->getIndex()=='asksheldon_color_image') ? 50 : 50));
            $src = $src->__toString();
            if(strpos($src, 'placeholder') == FALSE){
                return '< img src="'.$src.'" style="max-height:50px;max-width:50px;min-height:30px;min-width:30px;border: 1px solid #999;background:#efefef;" />';
            }
        }
        elseif($this->_getValue($row) != '' && $this->_getValue($row) != 'no_selection') {
            if(file_exists(Mage::getBaseDir('media').DS.'catalog/product'.$this->_getValue($row))) {
                return '< img src="'.$this->helper('catalog/image')->init($row, $this->getColumn()->getIndex())->resize((($this->getColumn()->getIndex()=='asksheldon_color_image') ? 50 : 50)).'" style="max-height:50px;max-width:50px;min-height:30px;min-width:30px;border: 1px solid #999;background:#efefef;" />';
            }
        }
        return '-';
    }
}

Afterwards you can add images in a Backend grid block class (f.e.: descendant of Mage_Adminhtml_Block_Catalog_Product_Grid) like that (in _prepareColumns):

$this->addColumnAfter('asksheldon_color_image',
    [
        'header'=> Mage::helper('catalog')->__('Colorimage'),
        'renderer'  => 'asksheldon_adminhtml/catalog_widget_grid_column_renderer_image',
        'index' => 'asksheldon_color_image',
        'filter'    => false,
        'sortable'  => true,
        'align' => 'center'
    ],
    'set_name'
);

$this->addColumnAfter('image',
    [
        'header'=> Mage::helper('catalog')->__('Mainimage'),
        'renderer'  => 'asksheldon_adminhtml/catalog_widget_grid_column_renderer_image',
        'index' => 'image',
        'filter'    => false,
        'sortable'  => true,
        'align' => 'center'
    ],
    'asksheldon_color_image'
);

$this->addColumnAfter('asksheldon_detail_image1',
    [
        'header'=> Mage::helper('catalog')->__('Detail 1'),
        'renderer'  => 'asksheldon_adminhtml/catalog_widget_grid_column_renderer_image',
        'index' => 'asksheldon_detail_image1',
        'filter'    => false,
        'sortable'  => true,
        'align' => 'center'
    ],
    'image'
);

$this->addColumnAfter('asksheldon_detail_image2',
    [
        'header'=> Mage::helper('catalog')->__('Detail 2'),
        'renderer'  => 'asksheldon_adminhtml/catalog_widget_grid_column_renderer_image',
        'index' => 'asksheldon_detail_image2',
        'filter'    => false,
        'sortable'  => true,
        'align' => 'center'
    ],
    'asksheldon_detail_image1'
);

$this->addColumnAfter('asksheldon_detail_image3',
    [
        'header'=> Mage::helper('catalog')->__('Detail 3'),
        'renderer'  => 'asksheldon_adminhtml/catalog_widget_grid_column_renderer_image',
        'index' => 'asksheldon_detail_image3',
        'filter'    => false,
        'sortable'  => true,
        'align' => 'center'
    ],
    'asksheldon_detail_image2'
);

$this->addColumnAfter('asksheldon_detail_image4',
    [
        'header'=> Mage::helper('catalog')->__('Detail 4'),
        'renderer'  => 'asksheldon_adminhtml/catalog_widget_grid_column_renderer_image',
        'index' => 'asksheldon_detail_image4',
        'filter'    => false,
        'sortable'  => true,
        'align' => 'center'
    ],
    'asksheldon_detail_image3'
);

$this->addColumnAfter('asksheldon_packing',
    [
        'header'=> Mage::helper('catalog')->__('Package'),
        'renderer'  => 'asksheldon_adminhtml/catalog_widget_grid_column_renderer_image',
        'index' => 'asksheldon_packing',
        'filter'    => false,
        'sortable'  => true,
        'align' => 'center'
    ],
    'asksheldon_detail_image4'
);

Leave a Reply

Your email address will not be published.

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.