8

Inside a block class which is being used to render a button in the admin panel, how to get the URL of the static content folder? Specifically, URL of the folder with images?

For example, in Company_Helloworld module an image for a custom button is located here:

app/code/Company/Helloworld/view/adminhtml/web/images/

Test 1

I tried this:

$url = $this->getViewFileUrl('Company_Helloworld::images/icon.png');

But it tries to retrieve the image from the Magento/backend theme:

http://example.com/pub/static/adminhtml/Magento/backend/en_US/Company_Helloworld/images/icon.png

while the image is not there, image is actually in the module's folder:

app/code/Company/Helloworld/view/adminhtml/web/images/icon.png

Test 2

And this:

$url = $this->getViewFileUrl('images/icon.png');

But it also tries to retrieve the image from the Magento/backend theme, this time without module context:

http://example.com/pub/static/adminhtml/Magento/backend/en_US/images/icon.png

Is there any way to put the image in the module and not in the backend theme?

8

Try to use:

echo $block->getViewFileUrl('Company_Helloworld::yourImage.png'); 

As a second argument of

\Magento\Framework\View\Element\AbstractBlock::getViewFileUrl($fileId, array $params = [])

you can set an array of next parameters: "module", "_secure", "theme", "area" and "locale"

  • Thank you, I tired this but it gives URL of assets from the Magento/backend theme. I extended my question with examples. – maginfortis Feb 22 '16 at 23:16
  • Run bin/magento setup:static-content:deploy devdocs.magento.com/guides/v2.0/config-guide/cli/… . The location looks correct from the first sight. Also look into devdocs.magento.com/guides/v2.0/architecture/view/… – Arkadii Chyzhov Feb 23 '16 at 5:54
  • This location is correct assuming that I put my image file inside a folder of Magento/backend theme. But I'd like to keep my image in my module's folder. Is it possible? – maginfortis Feb 23 '16 at 11:14
  • As I understood, you want to use an image in the admin panel, am I right? "Magento/backend" is a name of default admin theme, so that's correct theme for your example, than goes locale and module name. What does confuse you? Look for uses of the method getViewFileUrl in the code, and you will find working examples for blocks and templates. Run bin/magento setup:static-content:deploy and you should to find correlation between assets in the module and pub/static – Arkadii Chyzhov Feb 23 '16 at 13:27
3

Try to use:

<?php
class ...

protected $_assetRepo;

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

Now, In you case module name is : Company_Helloworld and you want icon.png from images directory so

echo $this->_assetRepo->getUrl("Company_Helloworld::images/icon.png");

Hope this will help you.

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.