I have installed two third-party modules and one local module. I want to rewrite/ extend the Helper/Data.php
of both third-party modules. Currently, I'm trying to do this from my local module's Helper/Data.php
.
My question, is it possible to have my single Helper/Data.php
extend both of the third-party helpers? So far, this is not working. What would be the best approach to this? I know I could fix this by creating another local module to rewrite the other third-party module, but since the functionality is related I'd rather stick to a single local module to accomplish it.
To add some detail, this is my rough setup:
MyCompany_Module/etc/config.xml
:
<helpers>
<moduleb>
<rewrite>
<data>MyCompany_Module_Helper_Data</data>
</rewrite>
</moduleb>
<modulea>
<rewrite>
<data>MyCompany_Module_Helper_Data</data>
</rewrite>
</modulea>
</helpers>
and in my module's MyCompany_Module_Helper_Data
:
class MyCompany_Module_Helper_Data extends PartyA_ModuleA_Helper_Data
{
...
}
class MyCompany_Module_Helper_Data extends PartyB_ModuleB_Helper_Data
{
...
}
So I am trying to extend two Helpers from a single Helper, is this possible/ an accepted approach? What is the generally accepted approach? Should I split this into a MyCompany_Module_Helper_Data1
and MyCompany_Module_Helper_Data2
, or even MyCompany_Module1_Helper_Data
and MyCompany_Module2_Helper_Data
Thanks
Best Answer
As far as I know, this is not at all possible with magento. Basically what you need is to inherit your class from
HelperA
(of module A) andHelperB
(of module B).So if you really need to extend from
HelperA
andHelperB
at the same time, then the best shot would be..Extend your your
Helper
fromHelperA
and then rewriteHelperA
class in such a way that it extends fromHelperB
instead of default base classMage_Core_Helper_Abstract
OR
Extend your your
Helper
fromHelperB
and then rewriteHelperB
class in such a way that it extends fromHelperA
instead of default base classMage_Core_Helper_Abstract
.This way your
Helper
will extend from bothHelperA
andHelperB
. Which method should opt in this case is purely depends upon your application. So choose the right option. Here I am going to show you how to do the first implementation.File :
app/code/local/Namespace/Module/etc/config.xml
File :
app/code/local/Namespace/Module/Helper/Data.php
File :
app/code/local/Namespace/Module/Helper/Rewrite/NamespaceA/ModuleA/Helper/Data.php
Explanation
Here
Namespace_Module
stands for your module.NamespaceA_ModuleA
stands forMODULE A
andNamespaceB_ModuleB
stands forMODULE B
.Your module's
config.xml
file holds two important definitions. First it defines your modules helper classNamespace_Module_Helper_Data
. Secondly, it rewritesHelperA
with a custom classNamspace_Module_Helper_Rewrite_NamespaceA_ModuleA_Helper_Data
. This way magento will useNamspace_Module_Helper_Rewrite_NamespaceA_ModuleA_Helper_Data
class when a request for helper class ofMODULE A
get processed.Now see the class definitions. Firstly, your helper class extends
Namspace_Module_Helper_Rewrite_NamespaceA_ModuleA_Helper_Data
. This means your class is now inheritingHelper A
. SecondlyNamspace_Module_Helper_Rewrite_NamespaceA_ModuleA_Helper_Data
extends fromHelperB
. Due to this now your helper class inherit fromHelperB
too.ie Your helper class inherits from
HelperA
which then inherits fromHelperB
.This is purely theory. I didn't try this before. But it seems really logical for me.
Note 1 : Use this approach only in the case, if you really want to define only one class which should extend from two base classes. Otherwise you can go for @Rick Method.
Note 2 : For the proper working of your module, you need to make sure two things here. First, your module should load only after
MODULE A
andMODULE B
. Second,MODULE B
should load beforeMODULE A
. That is the order of loading should be..Good luck