Magento 2 : Add custom script just after head tag
I found a simple way of doing it that seems very reasonable, because it uses existing features in the adminhtml
theme without any need to override/intercept anything.
Background
Like @sanchit-gupta mentioned, the root.phtml
template is used as the basis to render both the frontend
and adminhtml
areas, and so is the associated class \Magento\Framework\View\Result\Page
, which always looks for a block named head.additional
before rendering.
This block exists in the frontend
layouts by default, but unfortunately it doesn't exist in the adminhtml
layout.
Solution
With that in mind, the current best way (as of 2.3.x) to add inline <scripts>
to the adminhtml
's <head>
section is to add any head.additional
block: it will be automatically rendered by the framework. It either has to be a ListText
block so that it renders all it's children automatically, or a Template
with a specified template file. I actually chose too nest my actual block inside the ListText
block just to keep the same mechanism available as in the frontend
area (i.e. for compatibility with other plugins that might be doing the same).
Example
In your custom module or theme, add a layout update that inserts the following block into the body
of the page layout (which is just to do it the same way as it's done in Magento 2 core for the frontend
area):
<body>
<block class="Magento\Framework\View\Element\Text\ListText" name="head.additional">
<block name="your_block" class="Magento\Framework\View\Element\Template" template="Your_Module::your/template.phtml" />
</block>
</body>
Done! Your template will render inside <head></head>
– without any awkward overrides or "hacks".
What's more, if other modules also reference head.additional
in the adminhtml
area, they will be compatible with your code.
I am not sure if this is the correct way or not, but I have got a lead.
By default magento 2 usees the root.phtml
file to setup head
content accordingly, which is located in vendor/magento/module-theme/view/base/templates/root.phtml
(if it has not been overridden).
Here, the $requireJs
variable is loaded first in the head block. The $requireJs
variable is defined in the render
method inside Page
class -which is located in vendor/magento/framework/view/Result/Page.php
.
In this file, $requireJs
contains the require.js
block. And the require.js
block is defined in 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" />
Solution
1) Copy require_js.phtml
from vendor/magento/module-theme/view/frontend/templates/page/js
to your theme app/design/frontend/{VENDOR}/{THEME_NAME}/Magento_Theme/templates/page/js/
2) Now you can add your script like this:
<script>
console.log("I'm loaded!");
var require = {
"baseUrl": "<?php /* @escapeNotVerified */ echo $block->getViewFileUrl('/') ?>"
};
</script>
UPDATE (Using Module)
Overriding the require.js
block is not an elegant solution. if anyone has a good solution please answer. For now edit your layout xml:
<referenceBlock name="require.js">
<action method="setTemplate">
<argument name="template" xsi:type="string">Custom_Module::success/head.phtml</argument>
</action>
</referenceBlock>
and inside success/head.phtml
add your code:
<script>
console.log("I'm loaded!");
var require = {
"baseUrl": "<?php /* @escapeNotVerified */ echo $block->getViewFileUrl('/') ?>"
};
</script>