cancel
Showing results for 
Search instead for 
Did you mean: 

Get Media URL in Magento 2 or calling a Static Block.

Get Media URL in Magento 2 or calling a Static Block.

Hi All,

To get the dynamic domain and media url.

I want to get the media url or call static block in the following path:
Theme/Magento_Checkout/web/template - xxxxxxx.html file

Is it possible..
Please suggest a better solution for this..

Thanks,
Shiva.

13 REPLIES 13

Re: Get Media URL in Magento 2 or calling a Static Block.

Sorry, I don't get what you are asking. Would you try to express differently what you want to archieve?

Andreas von Studnitz, Trainer, Consultant, Developer and CEO of integer_net, Germany.

Re: Get Media URL in Magento 2 or calling a Static Block.

If I understood you correctly you would like to render image on one of your custom html template. In order to pass Media URL based on your domain you have to create ConfigProvider class and include into CompositeConfigProvider class from Magento\Checkout module.

 

 

    <type name="Magento\Checkout\Model\CompositeConfigProvider">
        <arguments>
            <argument name="configProviders" xsi:type="array">
                <item name="custom_config_provider" xsi:type="object">Vendor\Custom\Ui\ConfigProvider</item>
            </argument>
        </arguments>
    </type>

ConfigProvider class

namespace Vendor\Custom\Ui;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Checkout\Model\ConfigProviderInterface;

/**
 * Class ConfigProvider
 * @package     Vendor\Custom\Ui
 */
class ConfigProvider implements ConfigProviderInterface
{
    /**
     * @var Filesystem
     */
    private $filesystem;

    /**
     * ConfigProvider constructor.
     * @param Filesystem $filesystem
     */
    public function __construct(
        Filesystem $filesystem
    ) {
        $this->filesystem = $filesystem;
    }

    /**
     * @return array
     */
    public function getConfig()
    {
        return [
            'vendor_custom' => [
                'media_url' => $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA)->getAbsolutePath('path/to/file.jpg')
            ]
        ];
    }
}

In your custom JavaScript file you can access Media URL like above:

 

 

define(['uiComponent'], function(Component) {
var mediaUrl = window.checkoutProvider.vendor_custom.media_url;

return Component.extend({
defaults: {
template: 'Vendor_Custom/template',
},
getMediaUrl: function() {
return mediaUrl;
}
});
});

In template.html file simply use getMediaUrl() function.

 

<img data-bind="attr: {'src': getMediaUrl()}" />

Re: Get Media URL in Magento 2 or calling a Static Block.

Hi,
I am very new to magento, can you please explain in detail way. Means where i have to place all the above code.

And is this a core file modification..?

Re: Get Media URL in Magento 2 or calling a Static Block.

Hi @soho_rize

 

This is an example of extending core functionality by following best practices in Magento 2. 

All the mentioned code should be placed into the new custom module created by you inside the Magento 2 project directory: magentoRoot/app/code/Vendor/ModuleName.

Where Vendor is a name of a namespace let's say your company name or last name and ModuleName is a name of a feature where all code will exist. For further guidance on module creation please read post Magento 2 module in a separate repository.

 

First xml should be added into the app/code/Vendor/ModuleName/etc/di.xml file.

ConfigProvider class should be placed app/code/Vendor/ModuleName/Ui/ConfigProvider file.

JavaScript should be placed into app/code/Vendor/ModuleName/view/frontend/web/js/template.js file.

The template.html should be placed into app/code/Vendor/ModuleName/view/frontend/web/template/template.html file.

 

You should also add custom declaration of the Checkout "component" into the  app/code/Vendor/ModuleName/view/frontend/layout/checkout_index_index.xml file. It should be something like this, of course it depends where do you want to include your Media URL

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="checkout" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="checkout.root">
<arguments>
<argument name="jsLayout" xsi:type="array">
<item name="components" xsi:type="array">
<item name="checkout" xsi:type="array">
<item name="children" xsi:type="array">
<item name="steps" xsi:type="array">
<item name="children" xsi:type="array">
<item name="shipping-step" xsi:type="array">
<item name="children" xsi:type="array">
<item name="shippingAddress" xsi:type="array">
<item name="children" xsi:type="array">
<item name="shipping-address-fieldset" xsi:type="array">
<item name="children" xsi:type="array">
<item name="media_url" xsi:type="array">
<item name="component" xsi:type="string">Vendor_Custom/js/template</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</argument>
</arguments>
</referenceBlock>
</body>
</page>

Re: Get Media URL in Magento 2 or calling a Static Block.

Hi Max,

Sorry for asking you, i need to do all the code only in my local host.
But while installing magento 2 in localhost iam getting the blank screen and when enabled ini_set('display_errors', 1); it shows the following error:
Parse error: syntax error, unexpected '.' in /var/www/html/lloyds_dev/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 93

I have not found the correct solution for this..
Can you help me..

Re: Get Media URL in Magento 2 or calling a Static Block.

Open the /var/www/html/lloyds_dev/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php line 93 and check whether you have some file edits. The vendor directory should not be edited.

Re: Get Media URL in Magento 2 or calling a Static Block.

Hi Max,

I am getting the following error even for the Magento 2.1.3 fresh download:

Parse error: syntax error, unexpected '.' in /var/www/html/magento213/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 93

Re: Get Media URL in Magento 2 or calling a Static Block.

What version of PHP do you have? It seems like it might be 5.5 or even less if you experience this error.

Re: Get Media URL in Magento 2 or calling a Static Block.

Hi Max,

PHP Version:
PHP 7.0.15-1+deb.sury.org~trusty+1 (cli) (built: Jan 20 2017 09:16:11) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.15-1+deb.sury.org~trusty+1, Copyright (c) 1999-2017, by Zend Technologies

MySQL Version:
mysql  Ver 14.14 Distrib 5.6.33, for debian-linux-gnu (x86_64) using  EditLine wrapper