Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

If I need to insert image inside admin form inside function _prepareForm I simply call this:

$this->getViewFileUrl("Company_Module::images/icon.png")

But this doesn't work if I call it within custom field renderer

<?php
namespace Company\Module\Block\Adminhtml\Renderer;
class Customtype extends \Magento\Framework\Data\Form\Element\AbstractElement{
  var_dump($this->getViewFileUrl("Company_Module::images/icon.png")); //returns null
}

What would be a proper way to get image url in this case?

share|improve this question

You need to inject this class into your constructor: \Magento\Framework\View\Asset\Repository Assigning the instance to $this->_assetRepo

Then you can make the getViewFileUrl method:

    public function getViewFileUrl($fileId, array $params = [])
    {
        try {
            return $this->_assetRepo->getUrlWithParams($fileId, $params);
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            return false;
        }
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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