cancel
Showing results for 
Search instead for 
Did you mean: 

How to add Custom JS with Configuration from DB in PHTML in Magento 2.1

How to add Custom JS with Configuration from DB in PHTML in Magento 2.1

The problem is that script tag only (imperative notation) is not working/passing the Magento Extension Quality Program Coding Standard with Warning ( Missing JS component initialization. Use "x-magento-init" or "x-magento-template".).

 

So question is how to add Custom JS file (thati is not JS component) that needs to get Configuration values from DB? 

 

Is it possible to use something like this in PHTML file:

 

 

/** @var ...\Block\MainBlock $block */
$config = $block->getConfig(); //Config array

<script type="text/javascript">
        
        var Config = {
            config1 : '<?php echo $config['config1']; ?>',
            config2 : '<?php echo $config['config2']; ?>';
        };
        
         //custom JS code
        
</script>

 

Tags (3)
1 REPLY

Re: How to add Custom JS with Configuration from DB in PHTML in Magento 2.1

Just to make it more clear, this code is working fine if you install it manually from Zip but not passing the Marketplace validation in technical review with same message like Magento EQP tool described above.

 

So what is it the way (recommended) of using Config values, that will be used in custom JS file which will be on every FE page?

 

Even if we use requires js or something to load custom JS file, how to add/pass php config values from Magento Admin (DB)?

 

Cmon folks it has to be some way this is the basic functionality...