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?
2 Answers 2
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/backendtheme. I extended my question with examples.maginfortis– maginfortis2016年02月22日 23:16:04 +00:00Commented Feb 22, 2016 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– Arkadii Chyzhov2016年02月23日 05:54:28 +00:00Commented Feb 23, 2016 at 5:54
-
This location is correct assuming that I put my image file inside a folder of
Magento/backendtheme. But I'd like to keep my image in my module's folder. Is it possible?maginfortis– maginfortis2016年02月23日 11:14:29 +00:00Commented Feb 23, 2016 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/staticArkadii Chyzhov– Arkadii Chyzhov2016年02月23日 13:27:14 +00:00Commented Feb 23, 2016 at 13:27
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.
Explore related questions
See similar questions with these tags.