0

I have added image field in dynamic rows like below

$this->addColumn(
    'image',
    [
    'label' => __('Image'),
    'renderer' => $this->getImageRenderer()
]);

private function getImageRenderer()
{
    if (!$this->imageRenderer) {
        $this->imageRenderer = $this->getLayout()->createBlock(
            \Vendor\Module\Block\Adminhtml\Blocks\Edit\Tab\ImageRenderer::class,
            '',
            ['data' => ['is_render_to_js_template' => true]]
        );
    }
    return $this->imageRenderer;
}

ImageRenderer class

<?php

namespace Vendor\Module\Block\Adminhtml\Blocks\Edit\Tab;

class ImageRenderer extends \Magento\Framework\View\Element\AbstractBlock
{
    /**
     * {@inheritdoc}
     */
    protected function _toHtml()
    {
        $html = '<input type="file" name="' . $this->getInputName() . '" id="' . $this->getInputId() . '" ';
        $html .= '<image name="' . $this->getInputName() . '" id="' . $this->getInputId() . '" />';
        return $html;
    }
}
 

Now I am able to browse image and save in database.

enter image description here

Now how to show uploaded image in the grid?

Most probable I would like to use _prepareArrayRow function like below to achieve it

protected function _prepareArrayRow(\Magento\Framework\DataObject $row)
    {
        if (isset($row['image'])) {
            $image = $row->getImage(); // image path here
            // how to add image src from here
    }

My question is how to add html properties like img src in _prepareArrayRow function?

0

Below function has worked for me to show image in admin form as well as grid:

public function render(\Magento\Framework\DataObject $row)
    {
        $storeViewId = $this->getRequest()->getParam('store');
        $item = $this->_myCustomModelFactory->create()->setStoreViewId($storeViewId)->load($row->getId());
        $_images = $item->getImage();
        $src     = $this->_storeManager->getStore()->getBaseUrl(
                    \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
                ) . $_images;

        return '<image style="max-width:100px;" src ="'.$src.'" alt="Preview" >';
    }
2
  • My class which is preparing column is extending \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray and render function is like public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element) { return parent::render($element); } . Do you know how to customize this render function?
    – amitshree
    Jun 12 '21 at 15:54
  • Sorry, I am not sure about that but this function is showing images on grid without any errors. Jun 14 '21 at 4:29

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.