1
<?php namespace Magneto\PopupQuestion\Ui\Component\Listing\Column;  
 use Magento\Framework\View\Element\UiComponentFactory;             
 use Magento\Framework\View\Element\UiComponent\ContextInterface; 
 class Thumbnail extends \Magento\Ui\Component\Listing\Columns\Column 
 {
  const NAME = 'thumbnail';

  const ALT_FIELD = 'name';

/**
 * @param ContextInterface $context
 * @param UiComponentFactory $uiComponentFactory
 * @param \Magento\Catalog\Helper\Image $imageHelper
 * @param \Magento\Framework\UrlInterface $urlBuilder
 * @param array $components
 * @param array $data
 */
public function __construct(
    ContextInterface $context,
    UiComponentFactory $uiComponentFactory,
    \Magento\Catalog\Helper\Image $imageHelper,
    \Magento\Framework\UrlInterface $urlBuilder,
    array $components = [],
    array $data = []
) {
    parent::__construct($context, $uiComponentFactory, $components, $data);
    $this->imageHelper = $imageHelper;
    $this->urlBuilder = $urlBuilder;
}

/**

 * Prepare Data Source
 *
 * @param array $dataSource
 * @return array
 */
public function prepareDataSource(array $dataSource)
{
    if (isset($dataSource['data']['items'])) {
        $fieldName = $this->getData('name');
        foreach ($dataSource['data']['items'] as & $item) {
            $product = new \Magento\Framework\DataObject($item);
            $imageHelper = $this->imageHelper->init($product, 'product_listing_thumbnail');
            $item[$fieldName . '_src'] = $imageHelper->getUrl();
            $item[$fieldName . '_alt'] = $this->getAlt($item) ?: $imageHelper->getLabel();
            $item[$fieldName . '_link'] = $this->urlBuilder->getUrl(
                'catalog/product/edit',
                ['productid' => $product->getEntityId(), 'store' => $this->context->getRequestParam('store')]
            );
            $origImageHelper = $this->imageHelper->init($product, 'product_listing_thumbnail_preview');
            $item[$fieldName . '_orig_src'] = $origImageHelper->getUrl();
        }
    }

    return $dataSource;
}

/**
 * @param array $row
 *
 * @return null|string
 */
protected function getAlt($row)
{
    $altField = $this->getData('config/altField') ?: self::ALT_FIELD;
    return isset($row[$altField]) ? $row[$altField] : null;
}}

enter image description here

3
  • @GPrathap I Am Trying The Same Way Using Xml File I am displaying admin grid but i am not able to pass a id when i click on image details it shows an error that Invalid product id. Should be numeric value greater than 0 Feb 18, 2019 at 7:46
  • @GPrathap as you can see in above screen shot i am fetching details from db and from entitiid i am fetching product name the same way i want to fetch image Feb 18, 2019 at 7:48

0

Your Answer

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

Browse other questions tagged or ask your own question.