I'm working on a custom theme for Magento. I'm trying to create a module for the theme that adds some code to the <head>
but i'm finding it difficult to get it working.
I've created my module under app/code/Vendor/ModuleName
and created the module.xml file and the registration.php
file which is as follows:
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_ModuleName',
__DIR__
);
I've installed the module and when I run php bin/magento module:status Vendor_ModuleName
it tells me that the module is enabled.
Next is the part I'm unsure about. I've just copied what I've seen in other themes but i'm unsure if it's correct or if i'm missing something. In app/design/frontend/Vendor/custom_theme
(which is my custom theme) I've created the following files:
app/design/frontend/Vendor/custom_theme/Vendor_ModuleName/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>
<referenceContainer name="head.additional">
<block class="Magento\Framework\View\Element\Template" name="custom_head" template="html/head.phtml" after="-"/>
</referenceContainer>
</body>
</page>
app/design/frontend/Vendor/custom_theme/Vendor_ModuleName/templates/html/head.phtml:
<script>alert('Hello');</script>
Now when my custom theme is enabled, I would expect this to add the code to the head and see the alert box, but it's not working. I'm unsure why. Do I have this set up correctly? Magento seems to be ignoring the module's layout file that I've placed in my custom theme directory.