0

I need to get the url of the image located in the module.

I need to run a Patch script because I am updating a cms block with an additional image.

The image path I am getting is localhost/pub/static/version1655667432/frontend/_view/en_US...

But when I look at the pub folder, the path should be localhost/pub/static/version1655667432/frontend/default/en_US...

    try {
        $this->_appState->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND);

    } catch (\Exception $e) {
        $this->_appState->getAreaCode();
    }

    $params = array('_secure' => $this->request->isSecure());
    $madeInUsaImgPath = $this->assetRepo->getUrlWithParams('Collins_ShowOnAll::images/made-in-usa.png', $params);

1 Answer 1

1

You don't need to create any Setup/Patch scripts

For create your own extension you just need to create 2 files in module

  1. Create registration.php

    <?php
    declare(strict_types=1);
    
    use Magento\Framework\Component\ComponentRegistrar;
    
    ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Acme_StackExchange', __DIR__);
    
  2. Create module declaration etc/module.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="Acme_StackExchange">
            <!-- optional dependencies -->
            <sequence>
                <module name="Magento_Catalog"/>
            </sequence>
        </module>
    </config>
    
  3. Create frontend resource like view/frontend/web/images/file.png

And access file with Acme_StackExchange/images/file.png from skin

If you have more specific question, please update your question or specify in comment

[Update]

The path to static file contains deploy version in URL and I don't suggest you to use full URL.

For CMS Block/Page you can use directive view

<img src="{{view url='Acme_StackExchange::images/file.png'}}" alt/>
or
<img src="{{view url='Acme_StackExchange/images/file.png'}}" alt/>

If you want to use in template

<img src="<?= $block->getViewFileUrl("Acme_StackExchange::images/file.png"); ?>" alt=""/>
2
  • I updated the questions description. Jun 19, 2022 at 20:10
  • See updated comment, i hope this will help Jun 20, 2022 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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