25

In Magento 2, is it possible to add an arbitrary bit of HTML to every frontend/cart page's <head/>?

I know I can add a block to the content/<body> area with code something like this

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="content">
            <block type="Pulsestorm\Test\Block\Head" name="pulsestorm.head" as="pulsestorm.head" template="pulsestorm/head.phtml"/>
        </referenceBlock>
    </body>
</page>

However, it's not clear if there's a similar block for adding content to the <head/> element of a page. I've looked through the dev docs site and the source code, and there's lots of references to new tags you can use with a <head/> tag

<head>
    <css.../>
    <js.../>
</head>

but I can't find any reference of a way to add arbitrary html/text to the <head/> section of a page in Magento 2.

1 Answer 1

30

not 100% sure about this, but I think you can add code to the head using this:

<body>
    <referenceBlock name="head.additional">
        <block class="Pulsestorm\Test\Block\Head" name="pulsestorm.head" as="pulsestorm.head" template="pulsestorm/head.phtml"/>
    </referenceBlock>
</body>

I know, it sounds strange to add code in the <body>but render it in the <head> but here is why I think so:

The "skeleton" of the page is determined by this template: https://github.com/magento/magento2/blob/develop/app/code/Magento/Theme/view/base/templates/root.phtml
This template echos $headAdditional (and other variables)
These variables are initialized in \Magento\Framework\View\Result::render() and the var $headAdditional looks like this:

'headAdditional' => $addBlock ? $addBlock->toHtml() : null,

where $addBlock is

$addBlock = $this->getLayout()->getBlock('head.additional');

Then I search for references to head.additional and found this in app/code/Magento/Catalog/view/frontend/layout/catalog_product_opengraph.xml

<body>
    <referenceBlock name="head.additional">
        <block class="Magento\Catalog\Block\Product\View" name="opengraph.general" template="Magento_Catalog::product/view/opengraph/general.phtml">
            <block class="Magento\Directory\Block\Currency" name="opengraph.currency" as="meta.currency" template="Magento_Catalog::product/view/opengraph/currency.phtml"/>
        </block>
    </referenceBlock>
</body>

I didn't test the actual code (I'm getting lazy) but it's the only lead I have.

3
  • 3
    In a year or two you'll be lazy enough to just ask a question on Stack Exchange when you have one :) The above worked perfectly (eye rolls and head shakes not withstanding) Dec 7, 2015 at 22:35
  • 5
    If representative: this is the butterfly flap preceding the tsunami of how much I'm going to dislike the way M2 names things. Dec 7, 2015 at 23:23
  • @BrendanFalkowski A tsunami of flapping butterflies each preceding a tsunami of drowning flapping butterflies each preceding a tsunami ;(
    – 00-BBB
    Sep 28, 2021 at 13:35

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