Merchant Stories

Magento Extension Development: How to Create an Extension

Jan 31, 2014 4 min read 289 views
Listen audio
Magento Extension Development: How to Create an Extension

Magento ® is unique in its flexible eCommerce Solution which is used for developing professional web stores. We offer a detailed review of what an extension for Magento ® is and how it is created.

General Magento Extension Development concepts

An extension is a specific area on a page which displays blocks, handles, inscriptions, and templates, etc., that are being used. We advise creating a special slide-out panel, this will significantly simplify your work with the console.

A console is a set of various devices (in our case, programs, tasks or extensions) which are used for an effective operation of a human-computer system.

First of all, it is necessary to develop a file structure. We will review real designations of names and classes used for development of the extension.

Let us look into the designation of a ‘namespace’. The namespace is a beginning of any file name or its space, it can be called a product of the extension. For example, all classes in Magento ® (we refer to internal ones now), which are developed inside our console, have the namespace ”Mage”, but we will call it “Linker”. Then, a special directory is created inside which that name will be located – app/code/community.

This CMS has such code pools as local, core and community. In the first one, we see what is developed in the community, in the second one – the internal code of the company, and the third one presents the changes made for a specific side, they are hidden and not to be shared.

After that we need to create another directory with a module named ‘Insider’ and place the following catalog inside it:

Insider
|-- Block
|-- controllers
|-- etc
|-- — config.xml
|-- Helper
|-- — Data.php
|-- Model

Data.php and config.xml are documents necessary to launch any code, none of the web platforms can work without these documents.

After that, we need to add the following code to config.xml file:

<?xml version="1.0"?> <!— you are viewing file XML! -->
  <config> <!— it’s a standard that there is only one root node -->
    <modules>
      <Linker_Insider> <!—introducing ourselves -->
        <version>0.1.0.0</version>
      </Linker_Insider>
    </modules>  
  </config>

This code allows us to ‘inform’ the designer of the system about the extension name and version. The file Data.php contains the following data:

<?php
class Linker_Insider_Helper_Data extends Mage_Core_Helper_Data {}

It is necessary to remember that the file called ‘helper’ is not used but its work is very important for correct system functionality.

All file names are based on Zend principle, i.e., when you see the file name you understand what kind of file this is and where it is located. To be precise, the designation of a specific file fully complies with the location of this file in CMS documentation, you just need to replace the lower gap with a slash ‘/’. Then we enable our extension. For this, we need to access etc/modules/Mage_All.xml and add the following lines between the module description (characteristics) and a closing tag </config>:

<Linker_Insider>
    <active>true</active>
    <codePool>community</codePool>
</Linker_Insider>

By these simple actions, we inform the system that we intend to enable the module (<active>true</active>) which is called (</Linker_Insider>) and is located in the standard pool (app/code/community).

After the extension is enabled we need to activate it.

Create a template file. Ready templates like ‘frontend’ are saved in the folder app/design/frontend/base/default/template. Inside that folder, we create a new directory named after the module (insider), and place the disclose.phtml file in that directory. The file should contain some text, e.g. «Hello, world», this is necessary to see and evaluate the work of the extension module. Then we tell our CMS where exactly this file should be located in the system. All templates with XML layout are located in app/design/frontend/base/default/layout. This is where we create a file insider.xml with the following content:

<?xml version="1.0"?>
  <layout>
    <default> <!—file must be visible from everywhere-->
      <block type="core/template" name="insider" template="insider/disclose.phtml"/>
    </default>
  </layout>

The part of text ‘’type’’ indicates which exact block, or its part, is going to control rendering of a created ‘template’. In our case, this work will be done by Mage_Core_Block_Template. type=«modulename/blockname» which can also be decrypted as Namespace_modulename_Block_blockname. It is considered that namespace is already indicated in the system and it is known where the files with names like ‘modulename’ are located. Mage, in order to use your own block it will be necessary to add a small piece of text into the code. The part of text ‘name’ is necessary to be used by other text attributes as a referrer, for example, like <reference> is used.

Insider.xml will not work independently that is why we add a few more lines into the config.xml file:

<frontend> <!—changes visible to the users -->
    <layout> <!—layout changes -->
        <updates> <!—confirmation that we wish to make the changes -->
            <insider> <!—module name -->
                <file>insider.xml</file> <!—file which contains the changes -->
            </insider>
        </updates>
    </layout>
</frontend>

After that, we go to the live site and reload the page. The inscription inside the file (remember, in our case, it is «Hello, world») should be somewhere in the area available for public view. If it does not appear you need to make sure that you are not viewing a cached page. The cache can be disabled in the admin panel: System -> Cache Management.

If necessary, the inscription can be located a little higher, but you first need to learn the layout creation in Magento ®.

Preliminarily, this CMS processes absolutely all files that are located in the ‘layout’ folder, and all of them are then gathered into a larger folder. Using attributes ‘before’ and ‘after’ we need to tell the system when exactly our file (block, module, etc.) should be inserted. Any block has to attribute ‘name’ in layout and you need to start with that attribute. Go to the tab page.xml where ‘after_body_start’ block is located. After we find it, in our file insider.xml in the tag <reference> we add the following:

<reference name="after_body_start"> <!—launch file, inscription or a module -->
    <block type="core/template" name="insider" template="insider/disclose.phtml"/>
</reference>

The code is shared in this article is taken from the site www.habrahabr.ru.

If you have any additional questions regarding the installation of an extension for Magento ®, please leave a comment and we will do our best to answer the questions as soon as possible.

That's where you contact us!

    By submitting this form you agree to GoMage's Terms of Use and Privacy Policy
    woo-hoo! Now its time to keep checking your inbox, as we will be getting in touch soon. Promise :)
    oops! Thanks. But it seems like some kind of technical issues stop you from meeting GOMAGE. Could you try again?