1

I have a column in my custom grid, I am getting image URL in that column, I want to show image instead of URL. My code of calling that column is:

$this->addColumn(
    'image',
    [
        'header' => __('Header Image'),
        'index' => 'image'
    ]
);

How can I achieve this?

1 Answer 1

1

Use frame_callback function to render image in grid. Renderer would be a best practice, but frame_callback is the easy one.

use this code

$this->addColumn(
    'image',
    [
        'header' => __('Header Image'),
        'index' => 'image',
        'frame_callback' => array($this, 'callback_image'),
    ]
);

And at the end of the file add this function

public function callback_image($value)
{
    if (empty($value)){
        return '';
    }
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $mediaUrl = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
    $width = 150;
    return "<img src='" . $mediaUrl . $value . "' width='" . $width . "'/>";
}

This should work

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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