Block > head.additional - What is the right way to implement

by Materix   Last Updated August 01, 2020 09:09 AM - source

What is the right way to implement it via default.xml or via default_head_blocks.xml? As I can see, the structure is different for modules from different providers.

As I understand it default_head_blocks should only be used for the pure header data (CSS, JS, Metatags - https://devdocs.magento.com/guides/v2.4/frontend-dev-guide/layouts/xml-manage.html#layout_markup_meta) and not for referencing body data. Or are both ways correct?

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
  <body>
    <referenceBlock name="head.additional">
        <block class="Magento\Framework\View\Element\Template" name="...." template="..." />
    </referenceBlock>
  </body>
</page>

I currently have the problem that we get the following error with some extensions:

[2020-08-01 08:35:13] main.INFO: Broken reference: the '{block_name}' element cannot be added as child to 'head.additional', because the latter doesn't exist [] []

I have created my own theme which is based on the Luma theme. There you will find the default.xml in the Magento_Theme>layout

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
  <body>
    <referenceBlock name="head.additional">
        <block class="Magento\Framework\View\Element\Template" name="newtheme.header.data" template="Magento_Theme::html/header/newtheme_header.phtml" />
    </referenceBlock>
   </body>
</page>

The newly added block is also output in the frontend, so head.additional should also exist. So I don't understand how such a error could happen.

Tags : magento2.3


Related Questions