1

I created one module. which have helper function which returns module is enabled or not? Now I want to check if this module is enabled or not in javascript for all pages on the frontend, for that, I want to set function value to config provider like "window.checkoutConfig.customvalue". Now I need like "window.catalog.customvalue" or any global javascript value which can be accessed from every page, How can I do this?

One more example for global config is "BASE_URL" when you write BASE_URL in the console it gives URL, I want the same functionality, just need to add that kind of variable in config.

I know how to set config provider in the checkout with "chckoutConfig" but I want to set that function value in common config provider so I can get that value in every js file or console.

If anyone has an idea about it?

1 Answer 1

4

Magento 2 has "head.additional" block, where you can set your variables globally.

Add below code in

app\code\Name\Module\view\frontend\templates\globalvar/globalvariable.phtml

?php
$globalConfig = [];
$globalConfig['FirstVariable'] = 'My first value';
$globalConfig['SecondVariable'] = 'My second value';
?>
<script>
    window.globalConfigData = <?php echo json_encode($globalConfig); ?>;
</script>

Then add below code in XML

app\code\Name\Module\view\frontend\layout\default.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="head.additional">
            <block class="Magento\Framework\View\Element\Template" name="globalconfigvariable" template="Name_Module::globalvar/globalvariable.phtml"/>
        </referenceBlock>
    </body>
</page>

Your Answer

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

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