Php Magento 2:在头标记后添加自定义脚本

Php Magento 2:在头标记后添加自定义脚本,php,xml,magento,magento2,Php,Xml,Magento,Magento2,我想在head标记开始之后添加自定义脚本 喜欢 Helper=>Custom\Module\Helper\Data.php namespace Custom\Module\Helper; class Data extends \Magento\Framework\App\Helper\AbstractHelper { /** * @param \Magento\Framework\App\Helper\Context $context */ protecte

我想在head标记开始之后添加自定义脚本

喜欢

Helper=>Custom\Module\Helper\Data.php
namespace Custom\Module\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{

    /**
     * @param \Magento\Framework\App\Helper\Context $context
     */
    protected $_request;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\App\Request\Http $request
    ) {
         $this->_request = $request;
        parent::__construct($context);

    }
     public function getConfigValue($value = '') {
        return $this->scopeConfig
                ->getValue($value,\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    }
    public function getTemplate()
    {
        if ($this->getConfigValue('custom_general/general/active') == 1) {
            $template =  'Custom_Module::checkout/success.phtml';
        } else {
            $template = 'Magento_Checkout::success.phtml';
        }

        return $template;
    }
}
di.xml=>etc\di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../vendor/magento/framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\Checkout\Block\Onepage\Success" type="Custom\Module\Block\Onepage\Success"/>
</config>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
    <referenceBlock name="require.js">
        <action method="setTemplate">
            <argument name="template" xsi:type="string">Custom_Module::success/head.phtml</argument>
        </action>
    </referenceBlock> 
    </body>
</page>
<script>
    console.log("I'm loaded!");

</script>

Layout Xml=>Custom/Module/view/frontend/Layout/default.Xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../vendor/magento/framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\Checkout\Block\Onepage\Success" type="Custom\Module\Block\Onepage\Success"/>
</config>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
    <referenceBlock name="require.js">
        <action method="setTemplate">
            <argument name="template" xsi:type="string">Custom_Module::success/head.phtml</argument>
        </action>
    </referenceBlock> 
    </body>
</page>
<script>
    console.log("I'm loaded!");

</script>

自定义_模块::success/head.phtml
Template=>Custom/Module/view/frontend/templates/success/head.phtml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../vendor/magento/framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\Checkout\Block\Onepage\Success" type="Custom\Module\Block\Onepage\Success"/>
</config>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
    <referenceBlock name="require.js">
        <action method="setTemplate">
            <argument name="template" xsi:type="string">Custom_Module::success/head.phtml</argument>
        </action>
    </referenceBlock> 
    </body>
</page>
<script>
    console.log("I'm loaded!");

</script>

log(“我已加载!”);
请帮我解决这个问题


提前谢谢。

我不确定这是不是正确的方法,但我有一条线索

默认情况下,magento 2使用
root.phtml
文件来相应地设置
head
内容,该内容位于
vendor/magento/module-theme/view/base/templates/root.phtml
(如果未被覆盖)

在这里,
$requireJs
变量首先加载到head块中。
$requireJs
变量在
Page
类中的
render
方法中定义,该类位于
vendor/magento/framework/view/Result/Page.php

在此文件中,
$requireJs
包含
require.js
块。
require.js
块在
vendor/Magento/module-theme/view/frontend/layout/default.xml
中定义:

<block name="require.js" class="Magento\Framework\View\Element\Template" template="Magento_Theme::page/js/require_js.phtml" />
更新(使用模块)

重写
require.js
块不是一个优雅的解决方案。如果有人有好的解决方案,请回答。现在编辑布局xml:

<referenceBlock name="require.js">
    <action method="setTemplate">
        <argument name="template" xsi:type="string">Custom_Module::success/head.phtml</argument>
    </action>
</referenceBlock> 

我发现了一种简单的方法,似乎非常合理,因为它使用了
adminhtml
主题中的现有功能,而不需要覆盖/拦截任何内容

背景 与前面提到的@sanchit gupta一样,
root.phtml
模板被用作呈现
frontend
adminhtml
区域的基础,相关的类
\Magento\Framework\View\Result\Page
,它在呈现之前总是寻找名为
head.additional
的块

默认情况下,
frontend
版面中存在此块,但不幸的是,
adminhtml
版面中不存在此块

解决方案 考虑到这一点,当前(从2.3.x开始)将内联
添加到
adminhtml
部分的最佳方法是添加任何
头。其他
块:它将由框架自动呈现。它必须是一个
列表文本
块,以便自动呈现其所有子项,或者是一个带有指定模板文件的
模板
。实际上,我选择将我的实际块嵌套在
ListText
块中,只是为了保持与
frontend
区域中相同的机制可用(即,为了与其他可能正在执行相同操作的插件兼容)

例子 在自定义模块或主题中,添加布局更新,将以下块插入页面布局的
正文
(其方式与在Magento 2 core中对
前端
区域所做的方式相同):


完成了!您的模板将在
内部呈现–没有任何令人尴尬的覆盖或“黑客”


此外,如果其他模块也在
adminhtml
区域中引用
head.additional
,则它们将与您的代码兼容

感谢重播,但我不想在require_js.phtml和root.phtml中添加脚本,因为我没有添加自定义主题,这是我的自定义模块。@SandipDutt!请检查更新的答案是否对您有帮助?@SandipDutt您可以分享您的代码以便更好地理解吗?好的,但是当我在default.xml中添加您的解决方案时,它会出现错误,例如无效的方法Magento\Framework\View\Element\Template for action set Template您可以在这里分享您的代码吗?