1

I am absolutely new to Magento 2 (1 week in). I am trying to develop a Magento module that lets the admin specify a string (token) and when the admin saves the module I would like the token to inserted together with a <script> tag into the <head> section of each page.

If the admin changes the token and saves the config, it would be nice if it can update the same <script> tag. Or even remove it if no token is specified.

The module UI is no problem and also the saving works fine and intercepting the token value (with PHP), but I got stuck on how exactly to control the that gets inserted.

I experimented with view/frontend/layout/default.xml and can influence the header, but this seems to be very static.

<?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">
    <head>
        <script src="https://some-external-website.com/external-script.js" 
                data-token="token_value" 
                src_type="url"></script>
    </head>
</page>

So basically I would like to change the value in data-token ("token_value") when the admin saves the config from the module, which I do from a Plugin\Config file:

    ...
    public function aroundSave(\Magento\Config\Model\Config $subject, callable $proceed) {
        // your custom logic
        $section = $subject->getSection();
        $returnValue = $proceed();
        $newConfigs = $this->scopeConfig->getValue($section);
        $token = $newConfigs["general"]["token_value"];

        // +++ here would like to write value of $token to the <script> tag +++
        
        return $returnValue;
    }
    ...

Any help appreciated!

0

Am I correct in assuming that you're storing this token value as a config value?

In that case you could replace your current plugin for config with a Magento block (https://devdocs.magento.com/guides/v2.4/frontend-dev-guide/layouts/layout-overview.html), which you add to the head.

In a Magento block you can access the config and directly insert your token value into your script tag for example. Like this:

Vendor/MyModule/Block/HeadJs.php

<?php
namespace Vendor\MyModule\Block;

use Magento\Store\Model\ScopeInterface;

class HeadJs extends \Magento\Framework\View\Element\Template
{
    public function getToken() {
        $token = $this->_scopeConfig->getValue('vendor/module/token', ScopeInterface::SCOPE_STORE);
        // ... Do stuff with token if needed
        return $token;
    }
}

Vendor/MyModule/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="Vendor\MyModule\Block\HeadJs" name="vendor_mymodule_headjs" template="headjs.phtml" />            
        </referenceBlock>
    </body>
</page>

Vendor/MyModule/view/frontend/templates/headjs.phtml

<script src="https://some-external-website.com/external-script.js" data-token="<?php echo $this->getToken(); ?>" src_type="url"></script>
3
  • Nice! Thank you for helping me get on the right track @sduif, I see the script tag appearing in the head section now but the token is still empty. I am wondering if I still have something missing. 1. In the HeadJs.php, do I need a constructor to define _scopeConfig? 2. The getValue path presumably needs to be replaced with the actual vendor name, module name and the id of the field for the token on the config page (It's not some kind of fixed name path) 3. Presumably I don't need my di.xml anymore as I don't need the Plugin/Config folder for the aroundSave. – artuzai Apr 19 at 14:22
  • You're correct in assuming the di.xml will no longer be needed. And there's no need to create a constructor with _scopeConfig as this is already defined through the block's parent classes. As for the empty token. Do you use the right config path in getValue()? You can see the exact path in the core_config table if you're unsure. – sduif Apr 19 at 14:42
  • it is working!!! Thank you so much for your help! – artuzai Apr 19 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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