2

I had a custom page where I had to rewrite page/page.phtml, and inside the template file I needed some methods. So I had to rewrite the base class Mage_Page_Block_Html. This is how my module's layout xml look liked:

  <vendorpage_index_index>
      <reference name="root">
          <action method="setTemplate">
              <template>page/vendor.phtml</template>
          </action>
      </reference>
  </vendorpage_index_index>

and the rewrite in the module's etc/config.xml:

    <blocks>
        <page>
            <rewrite>
                <html>OurCompany_Vendor_Block_Frontend_Vendor</html>
            </rewrite>
        </page>  
    </blocks>

And it worked perfectly. However, Later, I found that it might make a conflict, because it rewrites the block class which is used everywhere, and it might rewrite other modules' rewrites. I removed the above rewrite xml, trying to redefine the block name root

<vendorpage_index_index>

    <block type="vendor/frontend_vendor" name="root" output="toHtml" 
      template="page/vendor.phtml" />

<vendorpage_index_index>

But the problem was, I had to manually add millions of child blocks which were added to the root block manually. I suppose this is because of occupying the root block with a different type making the root forget all its children elements.

So I added the millions of blocks which were assigned to the root before the handle root occupation. Now I have a very long layout file full of duplicates,

<vendorpage_index_index>

    <block type="vendor/frontend_vendor" name="root" output="toHtml" 
      template="page/vendor.phtml">

       <millions of blocks ...>

    </block>
<vendorpage_index_index>

which Evils of Duplication (®) are chasing. How do I avoid the duplication?

1 Answer 1

0

Your assumption isn't right. Rewriting a block with a new type does not make the original block forget all its children elements

When you rewrite a block like you did in your config.xml everytime the page/html block is instantiated, your custom block is instantiated instead.

As long as you make your new custom block extend the original block you should not have to change the layout declaration for this block.

Thus you don't have to add the following to your layout:

<block type="vendor/frontend_vendor" name="root" output="toHtml" 
  template="page/vendor.phtml" />

Because the root block is natively defined for all pages in app/design/frontend/base/default/layout/page.xml :

<default translate="label" module="page">
    <label>All Pages</label>
    <block type="page/html" name="root" output="toHtml" template="page/3columns.phtml">
    ...
6
  • If my assumption is wrong, why wouldn't the phtml load its child elements? And manually adding the blocks solved my problem? Look, I extended the type class, it was not just the phtml. Many of my rendering logic is embedded in the class found by type="vendor/frontend_vendor"
    – shampoo
    Commented Jul 12, 2016 at 9:56
  • @shampoo well you should start by checking whether or not the module output is disabled in the backend. Also ensure the module itself is enabled. You can also add echo get_class($this); in your template file to ensure that your rewrite works. Commented Jul 12, 2016 at 9:58
  • My rewrite worked. The problem was that, I was worried if another module rewrote the Block, and my module re rewrote that, it's gonna be a mess. Sorry, I'll edit my question to make that clear.
    – shampoo
    Commented Jul 12, 2016 at 10:00
  • @shampoo well that's the problem with rewrites. Any module can rewrite the same block. If you know there's already a rewrite for this block you can either extend the rewritten block or merge the changes from the other rewrite into your rewrite Commented Jul 12, 2016 at 10:02
  • 1
    @shampoo well there's no other way unfortunately. Whatever you write, there's always a risk that someone else rewrites it ;) Commented Jul 12, 2016 at 10:06

Your Answer

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

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