Skip to main content

In this post I tried to write a little about base module structure, how to use module’s files and include some custom cases.

 – Which codepool is better for custom module?

1. If you need to create some new functionality and then share it in magento community, put the module in app/code/community. It is better to do this way, because when other developer will need to fix something or to extend the functionality, he will be able to do some overrides in app/code/local folder.

2. If you need to override something from core or community, you need to use app/code/local. Name it in the following way:
Mage/Catalog: create in local module Magenmagic/Catalog. It is better to do this way, even if you need to do a little fixes. Because your little fix has potentiality to become a big and independent module in the future. It will also be easier to find your overrides and fixes.

 – How to use files inside module’s directory?

1. Blocks:

Blocks are used not for business logic, but for logic, which helps to display the information on frontend in a better way.
There are two types of blocks: auto-rendered and manually-rendered.

Auto-rendered:
If a block has core/text_list type, all of its children will be displayed automatically.
A block of core/text type should contain text and should not be used for layout purposes. The block will automatically render itself.

Manually-rendered:
Other blocks should be displayed manually. In your template use getChildHtml function or $this->getLayout()->getBlock(‘block.name’)->toHtml().
If in the current layout xml file the block has an alias (as=’block.alias’) use the alias instead of name.

A block is extending Mage_Core_Block_Template will render a template.
A block is extending Mage_Core_Block_Abstract do not render template. Use into their callback functions like _beforeToHtml and _afterToHtml to output data.

If you want to display a block automatically use output=”toHtml”:

<block type=”company/myblockclass” output=”toHtml”/>

You can find more useful information here: https://blog.philwinkle.com/the-most-misunderstood-concept-in-magento/

2. data and sql:

These folders work in the same way as install scripts.
Put install scripts in sql folder if you make some structure changes: create new tables, add new coulumns and etc.
Use data folder for your scripts if you need to insert some text, add cms block, product or something like that programmatically.

3. controllers and Controller:

controllers are used for represention business logic actions for the given request (dispatch(), preDispatch(), postDispatch() methods) and delegate commands to other parts of the system.

In the folder Controller put Abstract classes.
In the folder controllers put all classes which are inherited from Abstract classes.

4. Helper:

Use helpers for different functions, which you can call from anywhere in your code. For example, if you have somewhere dublicated code, you can create the function and to put it in the helper. So you can edit that code just in one place now.

Create helper if you need to use translations in your website.

If you use construction to translate like this $this->__(‘Foo’), it will be hard to say from which module’s translation data yu are using.
But if you want to be sure that you use translation from, for example Mage_Catalog module, use Mage::helper(‘catalog’)->__(‘Foo’).

When you create your custom module and create it’s settings in etc/system.xml you need to create helper or you will get an error.

5. Model:

A “model” represents data like product, category, CMS Page, Review, etc.
A “resource model” is a class that does the actual fetching of data from Magento. Every model has a resource model that is used to load a single instance of a model from the database.
A “collection” is a class that loads an array like structure of multiple models based on a set of rules.

Collection can be a resource model.
When you want to load a specific item, you use a model. When you want to load a number of items, you use a collection resource model.
One thing to keep in mind about loading via a collection resource model is each individual model’s afterLoad method won’t be called automatically, which means some data may not be loaded.

About translation data:

Always in templates you need to write some text like this: <?php $this->__(‘Some Text’); ?>
Because if you have different languages, it will be easy to translate your text.
How can you translate your text if its not in template, but in js file, or in cms block in admin panel?

js file:

1. //javascript
alert(“<?php echo Mage::helper(‘mymodule’)->__(‘I have been translated.’) ?>”);
2.//javascript
alert(Translator.translate(‘I have been translated.’));
<script type=”text/javascript”>
Translator.add(‘I have been translated.’,'<?php echo Mage::helper(‘mymodule’)->__(‘I have been translated.’)?>’);
</script>
3.Alternatively you can add the jstranslator.xml in your module under etc/ folder. This file contains all the string that you want to translate through JavaScript. Here is the sample file:

<jstranslator>
<my_string translate=”message” module=”mymodule”>
<message>I have been translated.</message>
</my_string>
<my_another_string translate=”message” module=”mymodule”>
<message>I want to be translated.</message>
</my_another_string>
</jstranslator>
Now in your template or JS file you can use:

//javascript
alert(Translator.translate(‘I have been translated.’));

3 ways to Add JavaScript Translations in Magento

cms block:

You can create new construction like {{translate text=”text to translate”}} and you need to make it work.

Copy app/code/core/Mage/Core/Model/Email/Template/Filter.php to app/code/local/Mage/Core/Model/Email/Template/Filter.php

and add that function:

public function translateDirective($construction)
{
$params = $this->_getIncludeParameters($construction[2]);
$text = $params[‘text’];
return Mage::helper(‘page’)->__($text);
}

If you need to translate some text in existing block use in design and in your theme folder locale/your_language/translate.csv
If you need to use translate for some special module use prefix in your csv file:
“Magenmagic_Checkout::Register”,”Личные Данные”

These posts helped me a lot:
https://blog.philwinkle.com/the-most-misunderstood-concept-in-magento/
http://magento.stackexchange.com/questions/3523/google-cdn-jquery-with-local-fallback-in-magento-layout-xml
http://blog.belvg.com/magento-certification-module-structure.html
http://magento.stackexchange.com/questions/33125/why-do-classes-use-a-helper-for-translation-instead-of-this

3 ways to Add JavaScript Translations in Magento

Vladimir Repalo

Vladimir Repalo

Magento Developer at Mobecls, 8+ years of experience. Feel free to ask me anything about this post in the comments below.