Magento 2 – Show Image in Custom Grid in Backend

gridimagemagento2

I am trying to display image on custom grid in back-end. I have tried some example but anything is not working yet.

Grid.php (Contact/Modules/Block/Adminhtml/Contact is the location)

$this->addColumn(
        'photo',
        [
            'header' => __('photo'),
            'index' => 'photo',
            'class' =>'photo',
            'renderer'  => '\Contact\Modules\Block\Adminhtml\Inquiry\Grid\Renderer\Photo',
        ]
    ); 

Photo.php

<?php
namespace Contact\Modules\Block\Adminhtml\Inquiry\Grid\Renderer;

use Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer;
use Magento\Framework\Object;
use Magento\Store\Model\StoreManagerInterface;

class Photo extends AbstractRenderer
{
   private $_storeManager;
   /**
    * @param \Magento\Backend\Block\Context $context
    * @param array $data
    */
   public function __construct(\Magento\Backend\Block\Context $context, 
StoreManagerInterface $storemanager, array $data = [])
   {
       $this->_storeManager = $storemanager;
       parent::__construct($context, $data);
       $this->_authorization = $context->getAuthorization();
   }
   /**
    * Renders grid column
    *
    * @param Object $row
    * @return  string
    */
   public function render(Object $row)
   {
       $mediaDirectory = $this->_storeManager->getStore()->getBaseUrl(
           \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
       );
       $imageUrl = $mediaDirectory.'/Contact/Modules'.$this->_getValue($row);
       return '<img src="'.$imageUrl.'" width="50"/>';
   }
}

reference link for this example https://firebearstudio.com/blog/magento-2-image-rendering.html
Now it is showing

 Invalid block type: \Contact\Modules\Block\Adminhtml\Inquiry\Grid\Renderer\Photo
Exception #1 (ReflectionException): Class Contact\Modules\Block\Adminhtml\Inquiry\Grid\Renderer\Photo does not exist

Is there anything i need to add to display image in grid view ?

Best Answer

you were missing return '<img src="'.$imageUrl.'" />';

change Modules to Module in every occurrence. because your module name is Module.

Related Topic