2

I'm currently working on this question: How can I add category IDs as a class on the top menu?

So far, I have set up a new class file that extends Mage_Page_Block_Html_Topmenu

I've included the toHtml and _toHtml functions, and am just trying to return a simple string from those functions to make sure it works. I have also included a simple string saying "HTML Test" outside of the php tags, to check whether or not the file is being included.

Now, I can see "HTML Test" at the top of the page, so my new class file IS being included, but my functions toHtml and _toHtml are not being used.

Here's the details:

My new class file is /app/code/local/Coffeesh0p/Topmenuclasses/Block/Html/Topmenu.php

I have created a config.xml here: /app/code/local/Coffeesh0p/Topmenuclasses/etc/config.xml

config.xml contains the following:

<config>
<modules>
    <Coffeesh0p_Topmenuclasses>
        <version>1.0</version>
    </Coffeesh0p_Topmenuclasses>
</modules>
<global>
    <blocks>
        <page>
            <rewrite>
                <html_topmenu>Coffeesh0p_Topmenuclasses_Block_Html_Topmenu</html_topmenu>
            </rewrite>
        </page>
    </blocks>
</global>

And finally, my new Topmenu.php contains the following:

HTML Test

<?php

class Coffeesh0p_Topmenuclasses_Block_Html_Topmenu extends Mage_Page_Block_Html_Topmenu
{

    public function getHtml($outermostClass = '', $childrenWrapClass = '')
    {

        return "TEST1";

    }

    protected function _getHtml(Varien_Data_Tree_Node $menuTree, $childrenWrapClass)
    {

        return "TEST2";

    }
}

?>

So, why is this file being included but my functions are not being called?

EDIT - I'm using Ultimo theme, and it turns out they use their own menu instead of the default topmenu, so that's why nothing was happening.

Oh well, at least I have learned something about module creation and from the resulting discussion!

  • looks correct so far, turn on developer mode, so you see all error messages. Also, what happens instead? Is the old block called, or does it output nothing? – Flyingmana Feb 14 '14 at 15:47
  • Check your html code what is inside <div class="nav-container">? – oleksii.svarychevskyi Feb 14 '14 at 15:53
  • The original "top menu" is there, untouched by my code. – Synchronium Feb 14 '14 at 15:59
1

I would venture to say that your getHtml method is not being called or you're not issuing echo from the template file:

<?php $_menu = $this->getHtml('level-top') ?>
<?php if($_menu): ?>
<div class="nav-container">
    <ul id="nav">
        <?php echo $_menu ?>
    </ul>
</div>
<?php endif ?>

However, in general you should avoid rewriting the Topmenu classes as it is commonly referred to as 'the most overridden class in Magento'. I typically try to modify it via the Observer that is called immediately after the start of getHtml (~ line 71 in CE 1.8):

Mage::dispatchEvent('page_block_html_topmenu_gethtml_before', array(
    'menu' => $this->_menu,
    'block' => $this
));

This returns the block to you, which allows you to make your modifications as you see fit. If you're looking to affect the actual nesting or add classes to the topmenu tags I understand the challenge, especially with the concatenation of the li/span tags, but you can probably affect this by modifying in-place the method _getMenuItemAttributes, others.

  • 1
    Ow man. A downvote for @philwinkle. Who could have done such a thing? whoever you are, you don't know what you've done. – Marius Feb 14 '14 at 15:55
  • 2
    @marius with a snarky comment, too. I should "please check before answering". I guess good advice for anyone lol – philwinkle Feb 14 '14 at 15:56
  • 1
    Sorry for the "snarky" comment. but a simple "please check before answering" would have been no fun at all. :) – Marius Feb 14 '14 at 15:59
  • 2
    Just realised that my theme isn't using topmenu at all, and has replaced it with their own menu. So yeah, I guess you could say getHtml wasn't being echo'd. What shall I do - mark this as the correct answer or delete the question for being totally stupid? – Synchronium Feb 14 '14 at 16:13
  • 1
    Don't delete the question - it has some value, @John. I would either answer it yourself or mark this as answered because technically your template wasn't echoing ;) – philwinkle Feb 14 '14 at 16:15

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.