5

I used

<?php echo $this->getViewFileUrl('Vendor_Module::images/demo.jpg'); ?>
<?php echo $block->getViewFileUrl('Vendor_Module::images/demo.jpg'); ?>

But it's working only frontend, I want it in adminhtml block file.

I used this in renderer column of grid File:

namespace Vendor\Module\Block\Adminhtml\Product\Renderer;

use Magento\Framework\DataObject;

class FileIcon extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
{

    public function render(DataObject $row)
    {

        echo $this->getViewFileUrl('Vendor_Module::images/demo.jpg');
        echo $block->getViewFileUrl('Vendor_Module::images/demo.jpg');

        exit;
        //return $file;;

    }
}
  • try with $this->_storeManager->getStore()->getBaseUrl().'pub/static/frontend/Namespace/Modulename/en_US/images/demo.jpg'; – Rakesh Jesadiya Mar 28 '17 at 12:10
  • This works only when image available in static folder also this is not right way to get url. – Prince Patel Mar 28 '17 at 12:26
  • Vendor_Module is your custom module name. – Khoa TruongDinh Mar 29 '17 at 7:22
  • Did you try only with echo $this->getViewFileUrl('Vendor_Module::images/demo.jpg');? – Khoa TruongDinh Mar 29 '17 at 7:35
  • See my updated answer. We can try with $this->getViewFileUrl('Vendor_Module::images/demo.jpg'); – Khoa TruongDinh Mar 29 '17 at 8:28
9

Use \Magento\Framework\View\Asset\Repository class

protected $_assetRepo;

public function __construct(
    ...
    \Magento\Framework\View\Asset\Repository $assetRepo,
    ...
) {
    ...
    $this->_assetRepo = $assetRepo;
    ...
}

Now, you can get image URL by

echo $this->_assetRepo->getUrl("Vendor_Module::images/demo.jpg");
7

Take a look at the Magento Marketplace module:

vendor/magento/module-marketplace/view/adminhtml/templates/index.phtml

$block->getViewFileUrl('Magento_Marketplace::partners/images/magento-connect.png');

The image is vendor/magento/module-marketplace/view/adminhtml/web/partners/images/magento-connect.png

Our image should be under web:

view/adminhtml/web/images/demo.jpg

Try to get with the module prefix in your template:

<?php echo $block->getViewFileUrl('Your_Module::images/demo.jpg'); ?>

Remember to clear Magento Cache and may need to delete view_preprocessed folder.

[EDIT]

In your case, you can try with

$this->getViewFileUrl('Vendor_Module::images/image-demo.jpg');
  • yes I have already image in adminhtml/web folder but it's seems like $this->getViewFileUrl() or $block->getViewFileUrl() not working – Prince Patel Mar 29 '17 at 6:04
  • I have tested. I'm pretty sure it will work, did you try to echo and run static content deploy content? – Khoa TruongDinh Mar 29 '17 at 7:28
  • Check now I edited question with file where I try to get URL. – Prince Patel Mar 29 '17 at 7:28

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.