Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are helper classes mandatory for all Magento extensions?

Many extensions (including the one's I've written) include a helper class that just extend the abstract base class without adding any functionality. The helper usually looks like this:

class MyCompany_MyModule_Helper_Data extends Mage_Core_Helper_Abstract {
}

The extended class is therefore just used for things that the abstract class provides, especially for translations. On the other hand, all Block and Controller classes in Magento inherit the __() method for translations - and in an extension I'm currently developing I don't have the need to call the helper class even once.

Can I just delete the helper class and remove it from config.xml? I've tried it and the extension seems to work fine without, but due to Magento's complexity I'm always a bit worried that there are implications I'm not aware of.

like image 226
Llian Avatar asked Dec 22 '12 18:12

Llian


2 Answers

If you're creating a module from scratch, helper classes aren't strictly necessary. I usually skip creating one until it's needed.

However, if any XML file uses the module attribute to specify a translation module, that attribute needs to resolve to a valid helper. For example, in this core file

<!-- File: app/code/core/Mage/Catalog/etc/system.xml -->
<tabs>
    <catalog translate="label" module="catalog">
        <label>Catalog</label>
        <sort_order>200</sort_order>
    </catalog>
</tabs>

There's module="catalog". By specifying this attribute, the Magento system code that translates the label will look something like this

Mage::helper('catalog')->__('Label');

So, removing the helper from the catalog module would break parts of Magento.

(The single part class alias catalog is automatically converted to Mage::helper('catalog/data') by Magento system code)

This "helper to group translations" feature is used in many of Magento's XML files, not just system.xml (layout, widgets, etc.). Additionally, there are some systems in Magento that will infer and/or require the existence of a helper module for translations (Access Control, external API system,etc. )

Long Story Short: If you're creating a module from scratch, feel free to leave the helper out until you start getting errors that Magento can't instantiate a helper. Never remove an existing helper from a module, and if you want to make sure you're 100% compatible with assumptions other people might make, always include a Data.php helper class.

like image 96
Alan Storm Avatar answered Nov 03 '22 18:11

Alan Storm


Magento's Helper classes contain utility methods that will allow you to perform common tasks on objects and variables. http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-1-introduction-to-magento#6

Assuming that the Helper file is empty with no custom methods eg.

<?php 
class MagePal_SomeModule_Helper_Data extends Mage_Core_Helper_Abstract
{
}

Then some of things that may still be affected are:

  • system.xml - blank screen for your module in admin -> system -> config
  • $this->__('') - error in your .phtml template (for internationalization/translation)

So if your helper is empty, without a system config section and no translation then it 'maybe' ok to delete.

like image 27
Renon Stewart Avatar answered Nov 03 '22 16:11

Renon Stewart