1

My image path is

app/code/Vendor/Module/frontend/web/images/tool/test.png

How to get this image path?

Please see screenshot:

screenshot

2

You will get image by below code . I hope you have image name which you want there.

public function __construct(

    \Magento\Framework\View\Asset\Repository $assetRepo,
){
    $this->assetRepo = $assetRepo;
}

public function testImageUrl(){
    $url =  $this->assetRepo->getUrl("Namespace_Module::image.png");;
    return $url;
}
  • this url search for the image pub static which is not a way . – Puneet Nov 14 '17 at 6:21
2

If you wish to show the thumbnail in admin grid please create the file Thumbnail.php in the the directory

Vendor\Module\Ui\Component\Listing\Column;

<?php
namespace Vendor\Module\Ui\Component\Listing\Column;

use Magento\Catalog\Helper\Image;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Ui\Component\Listing\Columns\Column;

class Thumbnail extends Column
{
const ALT_FIELD = 'title';

/**
 * @var \Magento\Store\Model\StoreManagerInterface
 */
protected $storeManager;

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

/**
 * 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) {
            $url = '';
            if ($item[$fieldName] != '') {
                $url = $this->storeManager->getStore()->getBaseUrl(
                    \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
                ).$item[$fieldName];
            }
            $item[$fieldName . '_src'] = $url;
            $item[$fieldName . '_alt'] = $this->getAlt($item) ?: '';
            $item[$fieldName . '_link'] = $this->urlBuilder->getUrl(
                'module/item/edit',
                ['item_id' => $item['item_id']]
            );
            $item[$fieldName . '_orig_src'] = $url;
        }
    }
    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;
}
}

In you Ui Component Listing Create the column:

<column name="slider_image" class="Vendor\Module\Ui\Component\Listing\Column\Thumbnail">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
            <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/thumbnail</item>
            <item name="sortable" xsi:type="boolean">false</item>
            <item name="altField" xsi:type="string">title</item>
            <item name="has_preview" xsi:type="string">1</item>
            <item name="label" xsi:type="string" translate="true">Thumbnail</item>
            </item>
        </argument>
    </column>
0

Try following code:

$block->getViewFileUrl('Vendor_Module::images/tool/test.png');
  • this will not work on the admin grid section , Please see this screen shot and suggest a solution prntscr.com/ha6620 – Puneet Nov 14 '17 at 4:57
  • Are you using Ui Component or Grid.php for grid ? – Suman Pradhan Nov 14 '17 at 5:27
  • Yes I am using UI component – Puneet Nov 14 '17 at 5:40
  • Then you need to keep the images in pub/media folder and then follow this link. magento.stackexchange.com/questions/94321/… – Suman Pradhan Nov 14 '17 at 5:51
  • above code work well on the frontend but i need to call the images on the backend grid – Puneet Nov 14 '17 at 6:22
0

Your ui component xml should have this added:

<column name="image" class="Your\Modulename\Ui\Component\Listing\Column\Thumbnail">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/thumbnail</item>
            <item name="sortable" xsi:type="boolean">false</item>
            <item name="altField" xsi:type="string">title</item>
            <item name="has_preview" xsi:type="string">1</item>
            <item name="label" xsi:type="string" translate="true">Thumbnail</item>
        </item>
    </argument>
</column>

app/code/vendorname/Modulename/Ui/Component/Listing/Column/Thumbnail.php

 something similar to this:

<?php
namespace Your\Modulename\Ui\Component\Listing\Column;

use Magento\Catalog\Helper\Image;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Ui\Component\Listing\Columns\Column;

class Thumbnail extends Column
{
    const ALT_FIELD = 'title';

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;

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

    /**
     * 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) {
                $url = '';
                if($item[$fieldName] != '') {
                    $url = $this->storeManager->getStore()->getBaseUrl(
                        \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
                    ).'pathtoyourimage/'.$item[$fieldName];
                }
                $item[$fieldName . '_src'] = $url;
                $item[$fieldName . '_alt'] = $this->getAlt($item) ?: '';
                $item[$fieldName . '_link'] = $this->urlBuilder->getUrl(
                    'your_module/yourentity/edit',
                    ['yourentity_id' => $item['yourentity_id']]
                );
                $item[$fieldName . '_orig_src'] = $url;
            }
        }

        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;
    }
}

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.