6

I know that if I add a helper to my module I need to declare it in config.xml using:

<helpers>
    <module>
        <class>Namespace_Module_Helper</class>
    </module>
</helpers>

But, what if I want to add more helpers, how do I tell Magento about their existence?

2 Answers 2

9

Adding

<helpers>
    <helper_alias>
        <class>Namespace_Module_Helper</class>
    </helper_alias>
</helpers>

to the config.xml you are not defining one helper.

You just define alias for calling helpers using Mage::helper('helper_alias') method.

According to magento code convention module name is used as helper's alias.

Actually in tag you are defining helper prefix which will be associated with helper alias.

So

Mage::helper('helper_alias') is the same as new Namespace_Module_Helper_Date.

If you want to call some other helper.

For example Namespace_Module_Helper_Otherhelper, you just new to call

Mage::helper('helper_alias/otherhelper')

Update: How to rewrite helper?

In config.xml you can rewrite any existing magento helpers.

For example if you want to rewrite Mage_Catalog_Helper_Product_Compare you need to add

<helpers>
    ....
    <catalog>
        <rewrite>
            <compare>Namespace_Module_Helper_Compare</compare>
        </rewrite>
    </catalog>
</helpers>

Hope that will help you.

2
  • What if I want to add/rewrite a helper in a 'sub-namespace' (not quite sure how to call it), say Mage_Catalog_Helper_Product_Compare? Would <class>Mynamespace_Mymodule_Helper> include this as well? How would I call it?
    – fuumind
    Feb 16, 2014 at 20:43
  • 1
    Answer was updated. Feb 16, 2014 at 21:00
-1

In my case I had to rewrite two helpers:

  1. enterprise_wishlist module helper

  2. mage_wishlist module helper

In my config.xml I have added:

<helpers>
    <enterprise_wishlist>
        <rewrite>
            <data>Namespace_Modulename_Helper_Data</data>
        </rewrite>
    </enterprise_wishlist>
    <mage_wishlist>
        <rewrite>
            <data>Namespace_Modulename_Helper_Shared_Data</data>
        </rewrite>
    </mage_wishlist>
</helpers>

Also don't forget to add:

  1. Helper/Data.php

    <?php
    class Namespace_Modulename_Helper_Data extends Enterprise_Wishlist_Helper_Data
    {}
    
  2. Helper/Shared/Data.php

    <?php
    class Namespace_Modulename_Helper_Shared_Data extends Mage_Wishlist_Helper_Data
    {}
    

In this way you can change.

1
  • Please check before editing @7ochem if the code is correct then editing is not acceptable. Nov 27, 2015 at 13:50

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.