0

I need to get asset image url in block file

Here is my code:

I have image on this page: logo.svg

\Abc\Mkt\view\adminhtml\web\images

Abc\Mkt\Block\image.php

namespace Abc\Mkt\Block;
use Magento\Backend\Block\Template\Context;
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;
class Image extends Field
{
 /**
 * @var string
 */
 /**
 * @param Context $context
 * @param array $data
 */
 protected $_assetRepo;
 public function __construct(
 Context $context,
 \Magento\Framework\View\Asset\Repository $assetRepo, 
 array $data = []
 ) {
 $this->_assetRepo = $assetRepo;
 parent::__construct($context, $data);
 }
 /**
 * Remove scope label
 *
 * @param AbstractElement $element
 * @return string
 */
 public function render(AbstractElement $element)
 {
 $image = $this->_assetRepo->getUrl("Abc_Mkt::images/logo.svg");
 $html='<div class="image"><img src='.$image.' style="width:35%;float:right;padding-top: 10px;"></div>';
 return $html;
 }
}
asked Nov 15, 2019 at 6:45
16
  • You want to display image in frontend or backend ? Commented Nov 15, 2019 at 7:00
  • I want to display image in backend Commented Nov 15, 2019 at 7:01
  • @kishan Not getting the image. even distort the page Commented Nov 15, 2019 at 7:03
  • Your file is calling did you checked that ? Commented Nov 15, 2019 at 7:03
  • 1
    Print your url and exit and check url proper display or not. It should be working. Commented Nov 15, 2019 at 7:08

1 Answer 1

0

using this class injection, you can get your image link

Magento\Framework\View\Asset\Repository $repository,

in your function.

$this->_repo->createAsset('Abc_Mkt::images/logo.svg')->getUrl();

answered Nov 15, 2019 at 7:38
1
  • This is not working Commented Nov 15, 2019 at 8:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.