2

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 ?

  • you were missing return '<img src="'.$imageUrl.'" />'; – Qaisar Satti Oct 4 '16 at 11:34
  • It is saying undefined block type – Ramkishan Suthar Oct 4 '16 at 11:37
  • control is not going in Photo.php – Ramkishan Suthar Oct 4 '16 at 11:37
  • plz check ii have updated my question – Ramkishan Suthar Oct 4 '16 at 11:39
  • add block code completely.. – Qaisar Satti Oct 4 '16 at 11:39
2

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

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

  • module name is Modules not Module – Ramkishan Suthar Oct 4 '16 at 11:45
  • check you grid.php location you added by urself – Qaisar Satti Oct 4 '16 at 11:46
  • sorry it was typo module name is Modules – Ramkishan Suthar Oct 4 '16 at 11:48
  • please suggest any answer – Ramkishan Suthar Oct 4 '16 at 11:57
  • @Ramkishan did you checked the spelling mistake? and remove var/generation folder? – Qaisar Satti Oct 4 '16 at 11:59
1

Check render function it't look like

    public function __construct(
        \Magento\Framework\UrlInterface $urlBuilder,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        $data = []
    ) {
        $this->_storeManager = $storeManager;
    }

    public function render(\Magento\Framework\DataObject $row)
    {
        $imageName = $row->getRbBannerImage();
        if($imageName=="")
        {
            return "";
        }
        else
        {
            $mediaDirectory = $this->_storeManager->getStore()->getBaseUrl(
                \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
            );

            return "<img src='".$mediaDirectory.'banner/images'.$imageName."' width='75' height='75'/>";
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.