3

I can not find a way to add custom block before logo and all header elements.

 <reference name="top.container">
   <block type="company_name/module" name="page_header" before="-" />
 </reference>

will insert below logo and search and below top links. top.menu will also come after.

 <reference name="top.links">
   <block type="company_name/module" name="page_header" before="-" />
 </reference>

or

 <reference name="header">
   <block type="company_name/module" name="page_header"/>
 </reference>

doesn't work. Is there any way to accomplish that with layout xml?

1
  • If you found a working solution here please accept an answer to close this. :)
    – sv3n
    Aug 3, 2017 at 19:48

3 Answers 3

2

Before header there is another block called after_body_start that directly comes after body-tag.

<reference name="after_body_start">
    <block type="company_name/module" name="page_header" before="-" />
</reference>
0

Based off your code I presume you're working on Magento 1, please confirm if this is the case.

You are adding your block to content blocks, content blocks do not automatically render child blocks. So you will need to also render this block in a template file. I'll use the header as an example.

If you look in app/code/core/Mage/Page/Block/Html/Header.php on line 38 you should see the template path:

page/html/header.phtml

I've no idea why they have defined this in PHP rather than layout XML.

If you copy this template into your theme you can now call your block in header.phtml.

<?php echo $this->getChildHtml('page_header') ?>
2
  • It is not theme, I am building custom module that adds custom block to existing theme. I guess I don't have then any option to add custom block before header but still inside header without modifying template file. The closest option is to use top.container, but this will add block inside header below logo and top links.
    – JohnyFree
    Mar 24, 2017 at 12:39
  • I'm not sure how you can cleanly do this without template changes. Even if you can achieve it will everyone who uses the module have space next to the logo? Maybe they have a custom header and they're not using that block at all, I don't think you can cover every scenario within a module.
    – Ben Crook
    Mar 24, 2017 at 13:22
0

When you add a block directly inside header block, magento will put your block just after all of it's child block that are added via other layout xml files that are processed before your pinsearch.xml.

Basically a header block contains following blocks by default. This is added via page.xml which will get processed before your pinsearch.xml

<block type="page/html_header" name="header" as="header">
    <block type="page/template_links" name="top.links" as="topLinks"/>
    <block type="page/switch" name="store_language" as="store_language" template="page/switch/languages.phtml"/>
    <block type="core/text_list" name="top.menu" as="topMenu" translate="label">
        <label>Navigation Bar</label>
        <block type="page/html_topmenu" name="catalog.topnav" template="page/html/topmenu.phtml"/>
    </block>
    <block type="page/html_wrapper" name="top.container" as="topContainer" translate="label">
        <label>Page Header</label>
        <action method="setElementClass"><value>top-container</value></action>
    </block>
    <block type="page/html_welcome" name="welcome" as="welcome"/>
</block>

see top.container (which is the wrapper block that you are mentioning, I hope) block is loading here. So when we add your block directly under header block, it just come just below welcome (This is not accurate. But if we consider page.xml only, this is true :-))

So if you need your block just inside the wrapper block, then what you need to is include your block inside wrapper block itself

<default> 
   <reference name="header">
       <reference name="top.container">
            <block type="core/template" name="pinsearch" template="pinsearch/pinsearch.phtml"/>
       </reference>
     </reference> 
  <default> 

Now suppose you need to include the block as child of header block itself, but need to display above the wrapper block (or any other child block), then what you need to do is.

<default> 
   <reference name="header">
        <block type="core/template" name="pinsearch" as="pinSearch" template="pinsearch/pinsearch.phtml"/>
     </reference> 
  <default> 

Then go to app/design/frontend/<package>/<theme>/template/page/html/header.phtml and then specify your block wherever you needed to display it as like this

  <?php echo $this->getChildHtml('pinSearch') ?>

Hope that clears your doubt.. Happy coding sis :-)

Reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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