23

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.

26

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
    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) – Alan Storm Dec 7 '15 at 22:35
  • 4
    If representative: this is the butterfly flap preceding the tsunami of how much I'm going to dislike the way M2 names things. – Brendan Falkowski Dec 7 '15 at 23:23

protected by Community Feb 6 '17 at 15:02

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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