1

I'm trying to add a custom block for my module into the head block. I would like this block to be added on every page. Now, my problem is that the block is not rendered at all except if I add the following into one of the files under the design/frontend/<theme>/default/layout/ folder:

<block type="page/html_head" name="head" as="head">
  <block type="core/template" name="my_custom_block" template="my_custom_block/custom_block.phtml"></block>
</block>

The layout file for my custom module is located under design/frontend/base/default/layout/my_custom_block.xml and looks like this:

<?xml version="1.0"?>
  <layout version="0.1.0">
    <default>
      <reference name="head">
        <block type="core/template" name="my_custom_block" template="my_custom_block/custom_block.phtml"></block>
     </reference>
    </default>
  </layout>

I can verify that this file gets loaded by adding a <remove name="root" /> to the layout definition, which in turn will trigger an error when trying to load the page.

Of course I could simply add the block directly into one of the themes templates, but it would be much cleaner if the block is visible just by installing and enabling the module.

Note that I have added the following dependency in app/etc/modules/My_Custom_Block.xml file:

<?xml version="1.0"?>
<config>
  <modules>
    <My_Custom_Block>
        <active>true</active>
        <codePool>community</codePool>
        <depends>
            <Mage_Page/>
        </depends>
    </My_Custom_Block>
  </modules>
</config>
1

the head block is initialised by the Mage_Page module (in the page.xml layout file).

So, when you try to update the default handle with <reference name="head">, you have to ensure that your extension is loaded after the Mage_Page module.

For that, you have to add

<depends>
    <Mage_Page/>         
</depends>

in your module's xml declaration file (located in etc/modules).

  • In fact I already have the above dependency in my app/etc/modules/My_Custom_Block.xml file – Cyclonecode Feb 7 '18 at 10:12
  • Well, this is weird. Did you flush the cache since you have added the dependency ? If yes, can you check in the system.log or exception.log : maybe you will see some message like not valid template ... or something else. – Julien Loizelet Feb 7 '18 at 10:46
  • Yes, I have also totally disabled caching at it still not works as expected. – Cyclonecode Feb 7 '18 at 10:51
  • I checked the log files but there is no such messages 🙄 – Cyclonecode Feb 7 '18 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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